Jump to content

Assign Array?


algiuxas
 Share

Recommended Posts

Hello,

I need to assign data to array:

Global $Test[3][4]
$Array = "Test"
SetArrayData($Array,"Hello world!",1,3)
ConsoleWrite($Array[1][3]&@CRLF)
Exit
Func SetArrayData($Array,$Data,$1,$2)
    $Array[$1][$2] = $Data; <-- ???
EndFunc

I need that somebody would help me with that function :)

Edited by algiuxas

After switching years ago to Linux, sadly I don't use AutoIt anymore.

Link to comment
Share on other sites

You're trying to assign a value to an array that is not actually an array?

You'll need to test if the variable being passed is an array or not. If it's not then create a temporary array and assign it ByRef.

#include <Array.au3>

Global $aTest1[3][4]
Global $sTest2 = "Test"
Global $aTest3[1]

SetArrayData($aTest1, "This is Row 2, Column 3", 2, 3)
If (@Error) Then SetArrayError(@Error, "$aTest1")
SetArrayData($sTest2, "This is Row 9, Column 3", 9, 3)
If (@Error) Then SetArrayError(@Error, "$sTest2")
SetArrayData($aTest3, "This is Row 2, Column 2", 2, 2, False)
If (@Error) Then SetArrayError(@Error, "$aTest3")

_ArrayDisplay($aTest1)
_ArrayDisplay($sTest2)
_ArrayDisplay($aTest3)


Func SetArrayData(ByRef $aArray, Const ByRef $sData, Const ByRef $iRow, Const ByRef $iColumn, Const $ReDim = True)
    If (Not IsArray($aArray)) Then
        ConsoleWrite("$aArray is not an array" & @LF)
        If (Not $ReDim) Then Return SetError(-1, 0, False)
        Local $aTemp[$iRow + 1][$iColumn + 1]
        $aArray = $aTemp
        ConsoleWrite("Converted $aArray to proper array" & @LF)
    EndIf

    If ($iRow >= UBound($aArray, $UBOUND_ROWS)) Then
        ConsoleWrite("Assigning to row " & $iRow & " when rows in $aArray are " & UBound($aArray, $UBOUND_ROWS) & " | Index out of range" & @LF)
        If (Not $ReDim) Then Return SetError(1, 0, False)
        ReDim $aArray[$iRow + 2][$iColumn + 1]
        ConsoleWrite("Increased $aArray row count to " & UBound($aArray, $UBOUND_ROWS) & @LF)
    EndIf

    If ($iColumn >= UBound($aArray, $UBOUND_COLUMNS)) Then
        ConsoleWrite("Assigning to column " & $iColumn & " when columns in $aArray are " & UBound($aArray, $UBOUND_COLUMNS) & " | Index out of range" & @LF)
        If (Not $ReDim) Then Return SetError(2, 0, False)
        ReDim $aArray[$iRow + 1][$iColumn + 2]
        ConsoleWrite("Increased $aArray column count to " & UBound($aArray, $UBOUND_COLUMNS) & @LF)
    EndIf

    $aArray[$iRow][$iColumn] = $sData
    Return True
EndFunc

Func SetArrayError($iError, $sVar)
    Switch ($iError)
        Case -1
            MsgBox("", "SetArrayData Error", $sVar & " passed to SetArrayData was not an actual Array and $ReDim was set to false")
        Case 1
            MsgBox("", "SetArrayData Error", "Row passed to SetArrayData was out of range for the variable " & $sVar & " and $ReDim was set to false")
        Case 1
            MsgBox("", "SetArrayData Error", "Column passed to SetArrayData was out of range for the  " & $sVar & " and $ReDim was set to false")
    EndSwitch
EndFunc

 

Link to comment
Share on other sites

23 minutes ago, InunoTaishou said:

You're trying to assign a value to an array that is not actually an array?

You'll need to test if the variable being passed is an array or not. If it's not then create a temporary array and assign it ByRef.

#include <Array.au3>

Global $aTest1[3][4]
Global $sTest2 = "Test"
Global $aTest3[1]

SetArrayData($aTest1, "This is Row 2, Column 3", 2, 3)
If (@Error) Then SetArrayError(@Error, "$aTest1")
SetArrayData($sTest2, "This is Row 9, Column 3", 9, 3)
If (@Error) Then SetArrayError(@Error, "$sTest2")
SetArrayData($aTest3, "This is Row 2, Column 2", 2, 2, False)
If (@Error) Then SetArrayError(@Error, "$aTest3")

_ArrayDisplay($aTest1)
_ArrayDisplay($sTest2)
_ArrayDisplay($aTest3)


Func SetArrayData(ByRef $aArray, Const ByRef $sData, Const ByRef $iRow, Const ByRef $iColumn, Const $ReDim = True)
    If (Not IsArray($aArray)) Then
        ConsoleWrite("$aArray is not an array" & @LF)
        If (Not $ReDim) Then Return SetError(-1, 0, False)
        Local $aTemp[$iRow + 1][$iColumn + 1]
        $aArray = $aTemp
        ConsoleWrite("Converted $aArray to proper array" & @LF)
    EndIf

    If ($iRow >= UBound($aArray, $UBOUND_ROWS)) Then
        ConsoleWrite("Assigning to row " & $iRow & " when rows in $aArray are " & UBound($aArray, $UBOUND_ROWS) & " | Index out of range" & @LF)
        If (Not $ReDim) Then Return SetError(1, 0, False)
        ReDim $aArray[$iRow + 2][$iColumn + 1]
        ConsoleWrite("Increased $aArray row count to " & UBound($aArray, $UBOUND_ROWS) & @LF)
    EndIf

    If ($iColumn >= UBound($aArray, $UBOUND_COLUMNS)) Then
        ConsoleWrite("Assigning to column " & $iColumn & " when columns in $aArray are " & UBound($aArray, $UBOUND_COLUMNS) & " | Index out of range" & @LF)
        If (Not $ReDim) Then Return SetError(2, 0, False)
        ReDim $aArray[$iRow + 1][$iColumn + 2]
        ConsoleWrite("Increased $aArray column count to " & UBound($aArray, $UBOUND_COLUMNS) & @LF)
    EndIf

    $aArray[$iRow][$iColumn] = $sData
    Return True
EndFunc

Func SetArrayError($iError, $sVar)
    Switch ($iError)
        Case -1
            MsgBox("", "SetArrayData Error", $sVar & " passed to SetArrayData was not an actual Array and $ReDim was set to false")
        Case 1
            MsgBox("", "SetArrayData Error", "Row passed to SetArrayData was out of range for the variable " & $sVar & " and $ReDim was set to false")
        Case 1
            MsgBox("", "SetArrayData Error", "Column passed to SetArrayData was out of range for the  " & $sVar & " and $ReDim was set to false")
    EndSwitch
EndFunc

 

Thank You, but what I need to do if it's just array name? Example:

;...
Global $Array1[3][4]
Global $Array2[3][4]
Global $Array3[3][4]
;...
For $i = 1 to 3
    SetArrayData("Array"&$i,$i,1,1)
Next
;Random...
For $i = 1 to 3
    SetArrayData("Array"&Random(1,3,1),"Hello world!",Random(0,2,1),Random(0,3,1))
Next
;...

No, I'm not trying to assign a value to an array that is not actually an array.

Edited by algiuxas

After switching years ago to Linux, sadly I don't use AutoIt anymore.

Link to comment
Share on other sites

I think this is kind of what you're trying to accomplish then? Using Assign and Eval. You can't assign it in the actual function itself because you're trying to use the variables as strings. Eval and Assign don't actually assign/eval values in arrays, they just work on the variable itself. If you use Eval you can get the whole array, but you can't use ByRef in the SetArrayData function. Instead, return an array and use Assign to do what you want.

#include <Array.au3>

Global $aTest1
Global $aTest2
Global $aTest3
Global $ReDimArray[] = [False, True, True, False]

For $i = 1 to 3
    Local $iRow = Random(0, 4, 1)
    Local $iColumn = Random(0, 4, 1)
    Assign("aTest" & $i, SetArrayData(Eval("aTest" & $i), "This is Row " & $iRow & ", Column " & $iColumn & " for array $aTest" & $i, $iRow, $iColumn))
Next

_ArrayDisplay($aTest1, "$aTest1")
_ArrayDisplay($aTest2, "$aTest2")
_ArrayDisplay($aTest3, "$aTest3")

Func SetArrayData($aArray, Const ByRef $sData, Const ByRef $iRow, Const ByRef $iColumn, Const $ReDim = True)
    If (Not IsArray($aArray)) Then
        ConsoleWrite("$aArray is not an array" & @LF)
        If (Not $ReDim) Then Return SetError(-1, 0, False)
        Local $aTemp[$iRow + 1][$iColumn + 1]
        $aArray = $aTemp
        ConsoleWrite("Converted $aArray to proper array" & @LF)
    EndIf

    If ($iRow >= UBound($aArray, $UBOUND_ROWS)) Then
        ConsoleWrite("Assigning to row " & $iRow & " when rows in $aArray are " & UBound($aArray, $UBOUND_ROWS) & " | Index out of range" & @LF)
        If (Not $ReDim) Then Return SetError(1, 0, False)
        ReDim $aArray[$iRow + 2][$iColumn + 1]
        ConsoleWrite("Increased $aArray row count to " & UBound($aArray, $UBOUND_ROWS) & @LF)
    EndIf

    If ($iColumn >= UBound($aArray, $UBOUND_COLUMNS)) Then
        ConsoleWrite("Assigning to column " & $iColumn & " when columns in $aArray are " & UBound($aArray, $UBOUND_COLUMNS) & " | Index out of range" & @LF)
        If (Not $ReDim) Then Return SetError(2, 0, False)
        ReDim $aArray[$iRow + 1][$iColumn + 2]
        ConsoleWrite("Increased $aArray column count to " & UBound($aArray, $UBOUND_COLUMNS) & @LF)
    EndIf

    $aArray[$iRow][$iColumn] = $sData
    Return $aArray
EndFunc

 

Link to comment
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
 Share

×
×
  • Create New...