Sign in to follow this  
Followers 0
dwaynek

dynamic arrays

5 posts in this topic

i'd like to automatically create a 2 column dynamic array that say reads from a list or a file that contains eg:

ann, 23

bob, 12

carol, 56

doug, 62

and automatically inserts these values into an array.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Here is an example that shows how you could do:

#Include <File.au3>
#Include <Array.au3>

$servers = FileOpenDialog("Open txt files", @DesktopDir & "", "Text File (*.txt)", 1 )
If @error Then Exit

Global $loadArray, $Data
Dim $dataArray[1][2]
_FileReadToArray($servers, $loadArray)
;_ArrayDisplay($loadArray)
;_ArrayDisplay($dataArray)
For $i = 1 to Ubound($loadArray)-1
    $Data = StringSplit($loadArray[$i], ", ",1)

    ReDim $dataArray[$i+1][2]
    $dataArray[$i][0] = $Data[1]
    $dataArray[$i][1] = $Data[2]
    $dataArray[0][0] = $i
Next
_ArrayDisplay($dataArray)

Regards

Edited by newbiescripter

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

i'm getting an error when i run your script. it says:

line 17:

$dataArray[$i][1]=$Data[2]

Array variable has incorrect number of subscripts or subscript dimension range exceeded.

Edited by dwaynek

Share this post


Link to post
Share on other sites

i'm getting an error when i run your script. it says:

line 17:

$dataArray[$i][1]=$Data[2]

Array variable has incorrect number of subscripts or subscript dimension range exceeded.

The problem is blank or mis-formatted lines will not produce the expected StringSplit() array, so some error checking is required. Try changing this section:
Dim $dataArray[1][2] = [[0, ""]]
_FileReadToArray($servers, $loadArray)
For $i = 1 To UBound($loadArray) - 1
    If StringStripWS($loadArray[$i], 8) <> "" Then
        $Data = StringSplit($loadArray[$i], ", ", 1)
        If UBound($Data) = 3 Then
            ReDim $dataArray[UBound($dataArray) + 1][2]
            $dataArray[0] += 1
            $dataArray[$dataArray[0]][0] = $Data[1]
            $dataArray[$dataArray[0]][1] = $Data[2]
        EndIf
    EndIf
Next

:P


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Array that adapts to the number of items in your file..

$NumLines = _FileCountLines("TextFile.txt")
Dim $Array[$NumLines]

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