Jump to content

File to Array read random lines in array - (Moved)


Recommended Posts

I've been programing in Autoit for a few years now. I've always been able to word my questions in searches and easily find answers. One reason I program in AutoIt is the community. For some reason I can't find the answer or word my issue poorly. Way back in the day in basic I could load a text file into an array. A Variable name with number like item$(1) Item$(2). This way I could continually pull a line from the array. I can load the text file into an array through Autoit and list the whole array. How do I pull a single line out as a variable? Say I have a list of 10 lines that contain PC names. I want to see what is stored in line 2. I can do this working from opening a file but don't want to keep reading the file from the disk. I hope I've explained this well and appreciate any help or point in the right direction. 

Link to comment
Share on other sites

  • Moderators

Moved to the appropriate forum, as the Developer General Discussion forum very clearly states:

Quote

General development and scripting discussions. If it's super geeky and you don't know where to put it - it's probably here.


Do not create AutoIt-related topics here, use the AutoIt General Help and Support or AutoIt Technical Discussion forums.

Moderation Team

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

@workingfast : as your basic language knowledge tends to show it, you'd prefer line 1 in item$(1), line 2 in (2) etc...
So there's another function, named _FileReadToArray, which allows you to do it (please note the underscore at the beginning of the function, compared to the FileReadToArray function explained by Malkey)

Here is a simple script based upon  _FileReadToArray, the text file to be read is... the script itself !

#include <File.au3>
#include <MsgBoxConstants.au3>

Local $aArray
_FileReadToArray(@ScriptFullPath, $aArray)
_ArrayDisplay($aArray,"File content : " & $aArray[0] & " lines", "1:", 0, Default, "Line content")
Msgbox($MB_SYSTEMMODAL, "Line 2 in Index 2", "Line 2 = " & $aArray[2])

7 lines in this script, here are the results when you run it :

5b6ec272bb60b_FiletoArray1.jpg.d828096b0fbc264211df7597dd873938.jpg

5b6ec28da9dbb_FiletoArray2.jpg.9ed41864242710c369ebec6c5a5f0a96.jpg

You can use this script directly after replacing @ScriptFullPath with your text file name, then access line x with $aArray[x]

And if you want to know how many lines contains your text file, the answer is in $aArray[0]... or in Malkey's Ubound($aArray) - 1

Just choose the method you prefer and good luck :)

Edited by pixelsearch
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...