Sign in to follow this  
Followers 0
notta

Help me understand arrays and stringsplit

3 posts in this topic

#1 ·  Posted (edited)

Guys, can you help me out with arrays? Help me out with this example, but please don't give me the answer for now :whistle:

Lets say I have a csv file in the format of:

compute,info,machine type

I want to create an array or text file with just the computer name and nothing else. The problem is, the first 2 fields are variable in length, so stringright or stringleft can't help me here. Most of this is from another post, so I'm trying to understand what's going on here. I have msgbox statements in here to see what is going on.

example:

computer1, office 119b, laptop

computer2, ' ', desktop

computer3, office 3, laptop

After the array $aRecords is created from computer.csv each line is in the form I listed above. The first msgbox displays the entire first line in the array. Here is where I get fuzzy, the stringsplit basically makes an array now of $test[0] = number of elements, $test[1] = computer1, $test[2] = office 119b, $test[3] = laptop? Am I right? The next message box displays computer1.

The next iteration $x is incremented 2. Now the first msgbox displays the entire line 2 "computer2, ' ', desktop". So I was thinking that since $test is made into an array from the stringsplit command $test is actually $test[2] on this iteration, but the next message box is blank. I was thinking that $test would be than set equal to computer2. I tried using $test[$x] = StringSplit($aRecords[$x], ',') but it told me where I can go. Can you guys help me understand this? Sorry for the newbness. Thanks.

#include <file.au3>

If Not _FileReadToArray("computers.csv",$aRecords) Then
   MsgBox(4096,"Error", " Error reading log to Array     error:" & @error)
   Exit
EndIf
For $x = 1 to $aRecords[0]
    MsgBox(0, "", $aRecords[$x])
    $test = StringSplit($aRecords[$x], ',')
    MsgBox(0, $x, $test[$x])

Next
Edited by notta

Share this post


Link to post
Share on other sites



maybe this can help

#include <file.au3>

Dim $aRecords

If Not _FileReadToArray("computers.csv", $aRecords) Then
    MsgBox(4096, "Error", " Error reading log to Array     error:" & @error)
    Exit
EndIf

For $x = 1 To $aRecords[0]
    MsgBox(0, "x = " & $x, $aRecords[$x])
    $test = StringSplit($aRecords[$x], ',')
    For $i = 1 To $test[0]
        MsgBox(0, "i = " & $i, $test[$i])
    Next
Next

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Thanks for the reply Val. Well here is what I came up with. I can't believe I got this to work, so can someone explain to me what I wrote :whistle: After seeing what you wrote I still had the problem of figuring out how to assign the first value to another array of just computer names. Your loop outputted each part of each line, but I only wanted the first element. I than created a new array:

$new[$aRecords[0]]

but it would fail on the second loop stating I had an incorrect number of subscripts. I than changed my array to:

$new[$aRecords[0] + 1]

and it worked. Why I really don't know. My example text file only had 2 lines and as you can see my msgbox:

;msgbox(0, "", $aRecords[0])

verfified that I had 2 elements.

I than hard wired $new[$x] = $test[1] and than used a ExitLoop after it set the first element. Is that bad coding to do it that way?

Can you tell me why "For $j = 1 To $new[0]" would not work in the last loop? The script would just end on that line and not execute anything in the for loop. I thought $anyarray[0] is equal to the number of elements in the array? I just used "$aRecords[0]" instead, but I really don't understand why "$new[0]" didn't work. Anyway, if you guys can make this a little more clear, it would greatly be appreciated. Thanks.

#include <file.au3>

Dim $aRecords


If Not _FileReadToArray("computers.csv", $aRecords) Then
    MsgBox(4096, "Error", " Error reading log to Array   error:" & @error)
    Exit
EndIf

Dim $new[$aRecords[0] + 1]

;msgbox(0, "", $aRecords[0])

For $x = 1 To $aRecords[0]
  ;MsgBox(0, "x = " & $x, $aRecords[$x])
    $test = StringSplit($aRecords[$x], ',')
    For $i = 1 To $test[0]
;msgbox(0, "Elements of $test array", $test[0])
      ;MsgBox(0, "i = " & $i, $test[$i])
;MsgBox(0, "Value of x is: ", $x)
        $new[$x] = $test[1]
        ExitLoop
;msgbox(0, "New Array", $new[$i])
    Next
Next

;MsgBox(0, "Elements of $new array", $new[1])
;MsgBox(0, "Elements of $new array", $new[2])

FileOpen("c:\compnames.txt", 9)

;For $j = 1 To $new[0] ' Did not work. Would not execute, even once, the msgbox in the loop
For $j = 1 To $aRecords[0]; this resolved the issue
    
    FileWriteLine("c:\compnames.txt", $new[$j])
    
;msgbox(0, "Final array", $new[$j])
;msgbox(0, "Final array", $new[0])
Next
FileClose("c:\compnames.txt")
Edited by notta

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