jguinch

_ArrayDeclareFromString, _ArrayToDeclarationString, _ArrayShuffleMultiDim, _ArrayCompare, _ArrayEnumValues, _ArrayAssign, ...

5 posts in this topic

#1 ·  Posted (edited)

Hello all !

Here are some functions that I have created to manipulate multidimensionnal arrays. I think some of them can be useful.
Some functions are limited to 32 dimensions.

List of the functions :

_ArrayAssign : Assigns an array variable by name with the data.
_ArrayCompare : Checks if two array are identical (size or data)
_ArrayDeclare : Creates an empty array with the specified size.
_ArrayDeclareFromString : Creates an array from an array declaration type string.
_ArrayEnumValues : Returns all values and indexes of an array in a 2D array.
_ArrayEval : Returns the array from an array variable name, or the value of an array element by its name
_ArrayShuffleMultiDim : Shuffle the whole data of an array.
_ArrayToDeclarationString : Returns an array declaration string. The returned string can be used with _ArrayDeclareFromString.

Some examples :

_Example1()
_Example2()
_Example3()
_Example4()
_Example5()
_Example6()
_Example7()

Func _Example1()
    Local $aArray1 = _ArrayDeclareFromString("[ [ 1, 2, 3], ['a', 'b', 'c'], [True, False, Null], [], [0x10, 1.3, -10.2E-3] ]")
    _ArrayDisplay($aArray1, "_ArrayDeclareFromString")
EndFunc


Func _Example2()
    Global $aArray2 = [[1,2,""], [4,5,6]] ; Must be a global variable
    _ArrayDisplay($aArray2, "Before _ArrayAssign")
    _ArrayAssign("aArray2[0][2]", 3)
    _ArrayDisplay($aArray2, "After _ArrayAssign")
EndFunc

Func _Example3()
    Local $aValues = [ [1,2,3], ['a', True, False] ]
    _ArrayAssign("aArray3", $aValues)
    Local $aRet = _ArrayEval("aArray3")
    _ArrayDisplay($aRet, "_ArrayEval")
    MsgBox(0, "", "Value for aArray3[1][0] : " & _ArrayEval("aArray3[1][0]") )
EndFunc

Func _Example4()
    Local $aArray3 = [ [[ "000", "001", "002"], ["010", "011", "012"]] , [[ "100", "101", "102"], ["110", "111", "112"]]  ]
    Local $aArray = _ArrayEnumValues($aArray3)
    _ArrayDisplay($aArray)
EndFunc

Func _Example5()
    Local $aArray4 = [ [1, 2, 3], [], ['A string', "That's so ""cool"" !"], [0x10, 1.3, -10.2E-3] ]
    Local $sDeclaration = _ArrayToDeclarationString($aArray4)
    MsgBox(0, "", $sDeclaration)
EndFunc

Func _Example6()
    Local $aArray5 = [ [[ "000", "001", "002"], ["010", "011", "012"]] , [[ "100", "101", "102"], ["110", "111", "112"]]  ]
    Local $aBefore = _ArrayEnumValues($aArray5)
    _ArrayDisplay($aBefore, "Before shuffle")

    _ArrayShuffleMultiDim($aArray5)
    Local $aAfter = _ArrayEnumValues($aArray5)
    _ArrayDisplay($aAfter, "After shuffle")
EndFunc

Func _Example7()
    Local $aArray6 = [[1, 2], [3, 4]]
    Local $aArray7 = [["", 2], [3, 4]]
    Local $iRet = _ArrayCompare($aArray6, $aArray7)
    If $iRet Then
        MsgBox(0, "", "Arrays have exactly the same size")
    Else
        MsgBox(0, "", "Arrays do not have the same size and dimension.")
    EndIf

    $iRet = _ArrayCompare($aArray6, $aArray7, 1)
    If $iRet Then
        MsgBox(0, "", "Arrays have exactly the same size and values")
    Else
        MsgBox(0, "", "Arrays do not have the same size and values.")
    EndIf
EndFunc

 

 

ArrayMultiDim.au3

Edited by jguinch
update / bug correction
4 people like this

Share this post


Link to post
Share on other sites



I like the idea of _ArrayToDeclarationString() - that is a very useful concept. One suggestion - I think you could add more information about dimension limitations in the comments to these functions. How many dimensions does _ArrayToDeclarationString() cater for?

Share this post


Link to post
Share on other sites

Nice! :)

Local $aSubjects = [[["Art"],["Painting",2,7],["Music",2,3],["Drama",3,4],["Dance",1,10]],[["Science"],["Maths",6,6],["Physics",5,1],["Chemistry",5,6],["Biology",5,6]],[["Sport"],["Football",12,8],["Golf",4,0],["Snooker",1,1],["Chess",10,0]]]

 

Share this post


Link to post
Share on other sites

Thanks.
Now, you can do the reverse, build the array from a string :

$array = _ArrayDeclareFromString('[[["Art"],["Painting",2,7],["Music",2,3],["Drama",3,4],["Dance",1,10]],[["Science"],["Maths",6,6],["Physics",5,1],["Chemistry",5,6],["Biology",5,6]],[["Sport"],["Football",12,8],["Golf",4,0],["Snooker",1,1],["Chess",10,0]]]')
$aEnum = _ArrayEnumValues($array) ; useful for arrays > 2 dimensions
_ArrayDisplay($aEnum)

 

1 person likes this

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