Jump to content

Array Dimension Inconsistency?


Recommended Posts

I found a potential inconsistency in how arrays are defined and handled and would like an understanding as to why this is done this way. I can work around it for the most part, but it becomes a hassle.

The issue is how a 1 dimensional array is defined:

Dim $array[5] is not the same as Dim $array[5][1] but in my feeble programming mind should be.

The first one is a "lazy" way of writing the second one, but IMHO the language should treat them the same. But there are some functions that don't.

#include <Array.au3>

Dim $array1[5] = [1,2,3,4,5]
Dim $array2[5][1] = [[1],[2],[3],[4],[5]]

_ArrayDisplay($array1,"Array1")
_ArrayDisplay($array2,"Array2")

Local $string1 = _ArrayToString($array1)
Local $string2 = _ArrayToString($array2);causes an error

First, the syntax in defining the two arrays are different. Displaying the arrays via _ArrayDisplay shows them to contain the same information. However, accessing the information is different. Array2 must be explicit $array2[3] is not defined it must be written as $array2[3][0].

I actually found this with _SQLite_GetTable2d function when I only wanted one column returned. I kept getting problems when I went to access the results without explicitly putting the second dimension, though it is zero.

I have a couple of work around functions:

Func _My_ArrayStrip($arData)
    Local $arTemp, $i
    Dim $arTemp[Ubound($arData,1)]
    For $i = 0 To UBound($arTemp)-1
        $arTemp[$i]=$arData[$i][0]
    Next

    Return $arTemp
EndFunc

Func _My_ArrayIncrease($arData)
    Local $arTemp, $i
    If Ubound($arData,2) = 0 Then
        Dim $arTemp[Ubound($arData)][1]
        For $i = 0 To UBound($arTemp)-1
            $arTemp[$i][0]=$arData[$i]
        Next
        
        Return $arTemp
    Else
        Return $arData
    EndIf
EndFunc

While they do what they say, strip out the "extra" dimension reference or add the "extra" dimension it is a hassle and can still lead to confusion if the "extra" dimension is necessary or not.

Could someone please help me in understanding why the two seemingly similar definitions should be treated differently?

Bob

Edited by YellowLab

You can't see a rainbow without first experiencing the rain.

Link to comment
Share on other sites

I found a potential inconsistency in how arrays are defined and handled and would like an understanding as to why this is done this way. I can work around it for the most part, but it becomes a hassle.

The issue is how a 1 dimensional array is defined:

Dim $array[5] is not the same as Dim $array[5][1] but in my feeble programming mind should be.

The first one is a "lazy" way of writing the second one, ...

They are not the same, and are not equivalent in memory, because the structure for the second index is only present in the second example. The second index is not just defaulted to 0, it doesn't exist in the memory structure at all in the first example.

...but IMHO the language should treat them the same. But there are some functions that don't.

#include <Array.au3>

Dim $array1[5] = [1,2,3,4,5]
Dim $array2[5][1] = [[1],[2],[3],[4],[5]]

_ArrayDisplay($array1,"Array1")
_ArrayDisplay($array2,"Array2")

Local $string1 = _ArrayToString($array1)
Local $string2 = _ArrayToString($array2);causes an error

First, the syntax in defining the two arrays are different. Displaying the arrays via _ArrayDisplay shows them to contain the same information. However, accessing the information is different. Array2 must be explicit $array2[3] is not defined it must be written as $array2[3][0].

The presence of "Col 0" in the GUI created by the _ArrayDisplay() UDF hardly tells you anything about the underlying memory structure of the array. They are different structures that require different addressing.

I actually found this with _SQLite_GetTable2d function when I only wanted one column returned. I kept getting problems when I went to access the results without explicitly putting the second dimension, though it is zero.

So you found when you used incorrect syntax you got errors? Fascinating... :)

I have a couple of work around functions:

Func _My_ArrayStrip($arData)
    Local $arTemp, $i
    Dim $arTemp[Ubound($arData,1)]
    For $i = 0 To UBound($arTemp)-1
        $arTemp[$i]=$arData[$i][0]
    Next

    Return $arTemp
EndFunc

Func _My_ArrayIncrease($arData)
    Local $arTemp, $i
    If Ubound($arData,2) = 0 Then
        Dim $arTemp[Ubound($arData)][1]
        For $i = 0 To UBound($arTemp)-1
            $arTemp[$i][0]=$arData[$i]
        Next
        
        Return $arTemp
    Else
        Return $arData
    EndIf
EndFunc

While they do what they say, strip out the "extra" dimension reference or add the "extra" dimension it is a hassle and can still lead to confusion if the "extra" dimension is necessary or not.

Could someone please help me in understanding why the two seemingly similar definitions should be treated differently?

Bob

Because they are different objects with different structures in memory. You also ignored the fact that AutoIt's syntax supports more than two dimensions. All of the forms $avArray[1], $avArray[4][1], $avArray[1][1][1], and $avArray[1][1][1][1] are different in memory and an index for every dimension used is required to address them.

The most you could wish for is for AutoIt to magically interpret your intent and fix your bad syntax when it interprets your code.

Shoot that feature request off to Valik and let me know what happens...

:lmao:

P.S. Your test for how many dimensions are present is faulty. This gives the number of dimensions in an array:

$iDimCount = Ubound($avArray, 0)

:think:

Edited by PsaltyDS
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
Link to comment
Share on other sites

Edit: saltys explination is better :) so i deleted mine

Edited by LurchMan

Dating a girl is just like writing software. Everything's going to work just fine in the testing lab (dating), but as soon as you have contract with a customer (marriage), then your program (life) is going to be facing new situations you never expected. You'll be forced to patch the code (admit you're wrong) and then the code (wife) will just end up all bloated and unmaintainable in the end.

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...