Question

Simple piece of code, in which we want to store the element of array (which in turn is another array) in another variable:

Global $arr[1][2] = [ [1, 2] ]
Global $sub = $arr[0]

And we get

Array variable has incorrect number of subscripts or subscript dimension range exceeded.:
Global $sub = $arr[0]
Global $sub = ^ ERROR

If we write

Global $arr[1][2] = [ [1, 2] ]
Global $sub[2] = $arr[0]

We get

Missing subscript dimensions in "Dim" statement.:
Global $sub[2] = $arr[0]
Global $sub[2] = ^ ERROR

So simple task, but I didn't find the way how I can do it. Have no idea. Please, help.

Was it helpful?

Solution

You are creating a multi-dimensional array with 2 dimensions, not an array inside of an array. The difference between the two is as follows:

  • Multi-dimensional array:

    Local $arr[1][2] = [ [1, 2] ]
    Local $sub = $arr[0][0] ; value = 1
    
  • Array inside array:

    Local $firstArray[2] = [1, 2]
    Local $arr[1] = [ $firstArray ]
    ;Local $sub = $arr[0][0] ; This does not work
    
    Local $sub = $arr[0]
    $sub = $sub[0] ; value = 1
    

In most cases in AutoIt you will prefer a multi-dimensional array. An array inside another array creates a copy of the original array, so you lose some performance and modifications to the copy don't affect the original.

Finally, prefer to use the Local keyword to define variables instead of the Global keyword. If you declare variables with the Local keyword you avoid polluting the global namespace.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top