Sign in to follow this  
Followers 0
PitBullBlonde

Help with arrays

11 posts in this topic

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...

:ph34r:

Any and all suggestions greatly appreciated...


Corey J. HarperCorey_Harper@HotMail.com

Share this post


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


Who else would I be?

Share this post


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.

Share this post


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,

Ian


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

Share this post


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

<{POST_SNAPBACK}>

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()).

Share this post


Link to post
Share on other sites

That is why I said Array[0] is USUALLY the size and said that

ubound($array) will give you the array size plus 1


Who else would I be?

Share this post


Link to post
Share on other sites

This is the point this-is-me.. The size IS unbound($array), not unbound($array) - 1.

The array is indexed from 0, so the last element is unbound($array) - 1, but the size is indeed unbound($array), as Valik explained.

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

@ezzetabi: There is no unbound and nobody said size would be UBound()-1.

As you have said so many times: read the manual :ph34r::(

Edited by tuape

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Sorry, I meant array's last item + 1. I talk to (no offense) laymen every day and that is the easiest way to explain it.

Edited by this-is-me

Who else would I be?

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

$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.
     $LineText[$LineCount]=$Text
     $LineCount = $LineCount +1 
Wend
If $LineCount < 2 Then
     ReDim $LineText[1]
Else
     ReDim $LineText[$LineCount]
Endif
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...

Share this post


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

Share this post


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
Sign in to follow this  
Followers 0