Jump to content

StringSplit and array question


Recommended Posts

Guest JanesDaddy

I have a comma-seperated file with a name and a number, e.g.

John Doe,12345

Jane Doe,12346

that is being read one line at a time.

Here's my code;

; Is file empty?
; Read in lines of text until the EOF is reached
While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop

; Seperate name and badge number into an array
    $details = StringSplit($line, ",")
    If @error = 1 Then ExitLoop

    MsgBox(0, "AutoIt Automation data", "Name = " & $details[1] & @CRLF & "Badge Number = " & $details[2])

; $details[1] contains the name ... $details[2] contains badge number

Wend

My question is, does the second line of the input file become $details[3] and $details[4], or does it become a two-dimentional array, $details[2,1] and $details[2,2]? Worse yet, does the second line of data overwrite the first, so that there is only ever one $details[1] and one $details[2]?

Link to post
Share on other sites

Personally I load the entire file and just do the parse from there.

ex:

$file="c:\myfile.csv"
$filelines=StringReplace(FileRead($file,FileGetSize($file)) ,@crlf,@lf)
; the above line reads the entire file and changes @crlf to @lf for the parse on the next line
$line=StringSplit($filelines,@lf); make an array by lines

for $i= 1 to $line[0]; read each line 
$details=Stringsplit($line[0],",") make an array of each line
if $details[0]>1 then
MsgBox(0, "AutoIt Automation data", "Name = " & $details[1] & @CRLF & "Badge Number = " & $details[2])
endif
next

the $details array will get written over, but you can always pull it back up anytime you want by calling out the line# you want. I prefer to keep the whole thing in memory so that I only access the file once, as well as many other advantages.

On your example, details[1] would always be the name, details[2] would always be the badge number, and would be over written each time you read a line.

Edited by scriptkitty

AutoIt3, the MACGYVER Pocket Knife for computers.

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