Jump to content

Recommended Posts

Hi all,

This may be a dumb question - but hey, I'm a blonde and a newbie here. I am pulling txt files into an array, but at present I'm running a routine to count the lines in a file before I pull it into an array (and the line count is actually more than the actual field elements so I'm creating an array that's too big at present). This is because I don't know how else to figure out the number of items that will be in the array when I declare it. There must be a function or something I'm missing...


Any and all suggestions greatly appreciated...

Corey J. HarperCorey_Harper@HotMail.com

Link to post
Share on other sites

Do you want to put your text in array using the @crlf as delimeters?

If so you can just do that:

$text = FileRead('filename.txt',GetFileSize('Filename.txt'))
$text = StringReplace($text,@crlf,@lf)
$text = StringSplit($text,@lf)

So every position of the $text array will contain a line and the position [0] ( $line[0] ) the number of lines.

Link to post
Share on other sites

@PitBull: Do you want each line in the text file to be an element in the Array?

If so, Read each line of the text file:

$my_var = FileReadLine(C:\Filename.txt, LineNumber)

$my_var1 = $my_var1 & "/" & $my_var

Put that in a loop to read each line and combine into one variable ($my_var1)

Then Split the Variable into an Array:

$my_array = StringSplit($my_var1, "/")

No need to Dim the Array ahead of time.

Hope this helps, sorry if it dosen't,


"Blessed be the name of the Lord" - Job 1:21Check out Search IMF

Link to post
Share on other sites

Array[0] is usually the array size and ubound($array) will give you the array size plus 1


No, that's only when using StringSplit(). UBound() always returns the size of the array. The last element will always be at UBound()-1 (Since indexing starts at zero). StringSplit() happens to put the number of elements as element[0] as a convience. This doesn't mean all arrays will automagically have the same format (If this wasn't what you were implying in your post, then it was worded incorrectly as it sure sounded that way, because nobody mentioned StringSplit()).
Link to post
Share on other sites

$Array[0] can contain any information $Var could contain. StringSplit puts the number of elements it creates in there, but it could be anything. UBound is much more reliable as far as checking how large an array is. For an array with only one dimension, the function returns the number of elements in the array and the last element is that number, minus one, because arrays start numbering at 0, not 1.

Back to the original question:

You can use the ReDim keyword to change the size of the array on the fly.

Dim $LineCount=0; Number of lines read
Dim $LineText[99]; Lines of the file.  Make it big enough to handle a good sized text file.
Dim $fn; Function Handle
Dim $Text ; Current line from the file.

$fn = FileOpen("My File.txt", 0)
While 1
     $Text = FileReadLine($fn)
     If @Error <> 0 Then ExitLoop
     If UBound($LineText) <= $LineCount Then ReDim $LineText[$LineCount+99]; Add a whole bunch of lines a few times, rather than a few lines lots of times.
     $LineCount = $LineCount +1 
If $LineCount < 2 Then
     ReDim $LineText[1]
     ReDim $LineText[$LineCount]
Edited by Nutster

David Nuttall
Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius

AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...

Link to post
Share on other sites

Wow! :( I really got it all - I SO appreciate you guys taking the time to discuss this and work it out. This is going to be a big help in my automating of our help system - I can now pull a list of records generated by our applications, into Help & Manual, adding it to our keyword index - something I would be manualy copying and pasting adnauseam for every single topic, every application...

Hey, you guys have done such great work, I'm sending another one at ya! :ph34r:

Corey J. HarperCorey_Harper@HotMail.com

Link to post
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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...