Sign in to follow this  
Followers 0
v3rt1g0

Dim Array with Variable

4 posts in this topic

#1 ·  Posted (edited)

I don't understand what I'm doing wrong, and I can't seem to find more info on the proper syntax via the help file or these forums.

What is the proper way to create array elements using a variable containing a number?

If I want to dump data into $aName[1], $aName[2], $aName[$n].. How do I create the array $aName[1] thru $aName[$n], to be later populated as below?

The file the script is reading is created by a resident app that sits on several dozen machines, pushing machine status to a share.

If I run the following, I get this error:

C:\[...]\Studio_Machine_Status\machstat002.au3 (44) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:

$aName[$i] = FileReadLine($oFile, 1)

^ ERROR

I've tried putting a For loop in there to create $aName[1], $aName[2], etc and that doesn't work either.

I must be missing something fundamental here.

#include <Array.au3>
#include <File.au3>

Global $mArray, $i, $sFile, $oFile

UpdateStatusData()

Func UpdateStatusData()
    ;Check folder exists and dump contents to array
    If Not FileExists("\\share\dat\") Then MsgBox(0, "Error", "Can't find \\share\dat\")
    $mArray = _FileListToArray("\\share\dat\", "*.kh", 1)
    If @error = 1 or @error = 4 Then MsgBox(0, "Error", "Path not found or no files found at \\share\dat\")
    _ArrayDisplay($mArray) ;works fine
    ;dim arrays to # of files/machines present
    Global $aName[$mArray[0]], $aStat[$mArray[0]], $aOS[$mArray[0]], $aLang[$mArray[0]], $aIdle[$mArray[0]], $aApp[$mArray[0]]
    ;Populate arrays
    $i = 1
    For $i = 1 to $mArray[0]
        $sFile = $mArray[$i]
        $oFile = FileOpen("\\share\dat\"&$sFile, 0)
        $aName[$i] = FileReadLine($oFile, 1) ;Machine name  <-------------------------- Crash
        $aStat[$i] = FileReadLine($oFile, 2) ;Ready/Running/Dead
        $aOS[$i] = FileReadLine($oFile, 3) ;Which OS installed?
        $aLang[$i] = FileReadLine($oFile, 4) ;which lang OS installed?
        $aIdle[$i] = FileReadLine($oFile, 5) ;how many minutes idle?
        $aApp[$i] = FileReadLine($oFile, 6) ;are any known apps running?
        FileClose($sFile)
    Next
    _ArrayDisplay($aName)
EndFunc
Edited by v3rt1g0

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Dim $mArray[100]

replace 100 by the max number of element you'll have.. and do the same for other array variables

Edited by MikeP

Share this post


Link to post
Share on other sites

I don't understand what I'm doing wrong, and I can't seem to find more info on the proper syntax via the help file or these forums.

What is the proper way to create array elements using a variable containing a number?

If I want to dump data into $aName[1], $aName[2], $aName[$n].. How do I create the array $aName[1] thru $aName[$n], to be later populated as below?

If you know how many elements you want before you create the array, then it's just:
$n = 10; number of elements to have

Global $avArray[$n + 1] = [$n]; sets element [0] to the count

For $i = 1 to $avArray[0]

; do something on elements [1] thru [10]

Next

:)


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

Global $avArray[$n + 1] = [$n]; sets element [0] to the count

That did it. Thank you!

I see what I was doing wrong.

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