Jump to content

arrays copied when using operator "="


Recommended Posts

Hi all,

I read in AutoIT's manual that the code below causes $array2 to be completely duplicated before being affected to $array1. The 2 identifiers ($array1 and $array2) won't point to the same structure, they are enterily different structures in memory.

Dim $array2 [2] = ["0", "1"]
Dim $array1 = $array2

So if the code below is run...

$array1[0] = "66"

...Then here are the values that will be contained by the arrays : $array1 = {66,1} , $array2 = {0,1}

Now here is my issue. I have difficulties handling this :

Dim $array[1][2] = [["", ""]]
Dim $subArray[2] = ["66", "77"]

$array[0][1] = $subArray

My quesiton is : how do I change the value "66" or "77" only by having access to variable $array ???

In other languages you'd write something like : ($array[0][1])[0] , but I don't know in AutoIT script.

Link to comment
Share on other sites

lol you're complicating things. If an element contains array variable then you don't use ($array[0][1])[0]. You first assign it to a temporary variable and then access it's subscripts like:

$avTemp = $array[0][1]

If IsArray($avTemp) Then

$Something = $avTemp[0]

EndIf ;...

Link to comment
Share on other sites

You can use an ArrayEntry as ByRef-param in a function. You can use this to write functions to manipulate inner arrays.

Example for 1D and 2D inner arrays:

Func _GetSubVal(ByRef $InnerArray, $Dim1=0, $Dim2=0)
    ; Prog@ndy
    If UBound($InnerArray) <= $Dim1 Then Return SetError(1,0,0)
    Switch UBound($InnerArray,0)
        Case 1
            Return $InnerArray[$Dim1]
        Case 2
            If UBound($InnerArray,2) <= $Dim2 Then Return SetError(2,0,0)
            Return $InnerArray[$Dim1][$Dim2]
        Case Else
            Return SetError(3,0,0)
    EndSwitch
EndFunc

Func _SetSubVal(ByRef $InnerArray, $Dim1, $Value, $Dim2=0)
    ; Prog@ndy
    If UBound($InnerArray) <= $Dim1 Then Return SetError(1,0,0)
    Switch UBound($InnerArray,0)
        Case 1
            $InnerArray[$Dim1] = $Value
            Return 1
        Case 2
            If UBound($InnerArray,2) <= $Dim2 Then Return SetError(2,0,0)
            $InnerArray[$Dim1][$Dim2] = $Value
            Return 1
        Case Else
            Return SetError(3,0,0)
    EndSwitch
EndFunc

Dim $SubArray[2] = ["test","test2"]
Dim $array[2] = [$SubArray,"000"]
$SubArray = 0

MsgBox(0, '', _GetSubVal($array[0],1))
_SetSubVal($array[0],1,"set")
MsgBox(0, '', _GetSubVal($array[0],1))

Dim $SubArray[2][2] = [[ "[0,0]", "[0,1]"], [ "1,0", "1,1" ]]
Dim $array[2] = [$SubArray,"000"]
$SubArray = 0


MsgBox(0, '', _GetSubVal($array[0],0,1))
_SetSubVal($array[0],0,"set [0,1]",1)
MsgBox(0, '', _GetSubVal($array[0],0,1))

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

$avTemp = $array[0][1]

If IsArray($avTemp) Then

$Something = $avTemp[0]

EndIf ;...

You didn't understand my need. That's why I first explained that an array is copied every time you use operator "=".

If I use temporary variable $avTemp, then it will not contain $array[0][1], it will contain a copy of it. Therefore, if I change something in $avTemp, it will not be changed for real in $array. I want to change the values in the array stored in $array[0][1].

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...