Jump to content

Assign function with arrays


Recommended Posts

Dim $strTestReference, $arrTestArray[3][3][3]
$strTestReference = StringFormat("$arrTestArray[%d][%d][%d]", $x, $y, $z)
Assign($strTestReference, $strValue)

I'm just curious why Assign can't be used with arrays so that something like the code above could be accomplished (just thrown together, not what I'd actually do). If it did, it would be easy to create UDFs to populate and display arrays of unlimited dimensions (within the bounds of variable limits). I'm not sure where an array with more than a handful of dimensions would be needed, I'm just curious about it more than trying to prove a point.

Probably moreso than using it in AutoIt, I'm curious what prevented it in the implementation of the Assign function. Is it limitations of the C++ language or compiler?

My UDFs: ExitCodes

Link to comment
Share on other sites

All you need is to Eval() the entire array to a known temp variable then Assign() it back.

#include <Array.au3>

Global $arrTestArray[3][3] = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
_ArrayDisplay($arrTestArray, "Before")

$strTestReference = "arrTestArray"
$strValue = "Changed!"
$aTemp = Eval("arrTestArray")
$x = Random(0, UBound($aTemp) - 1)
$y = Random(0, UBound($aTemp, 2) - 1)
$aTemp[$x][$y] = $strValue

Assign($strTestReference, $aTemp)
_ArrayDisplay($arrTestArray, "After")

:unsure:

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

Interesting. The logic seems sound. Below is what I was originally thinking. The goal being to create an _ArrayDisplay type function that would work for an array of any dimensions. My problem area is between the ========= blocks.

; My preferred variable nomenclature:
; $gxVariable = global variable of type x
; $lxVariable = local variable of type x
; $axVariable = variable of type x, passed as an argument
; x being s=str, n=int, b=bool, a=array, etc.

Global $gaTestArray[3][3][3] = [[[1,2,3],[4,5,6],[7,8,9]],[[10,11,12],[13,14,15],[16,17,18]],[[19,20,21],[22,23,24],[25,26,27]]]

ConsoleWrite(_ArrayGetString($gaTestArray))

; This would be the user-called function so the user can call _ArrayGetString($array)
; instead of having to create the dummy string and counter array variables since they're ByRef
Func _ArrayGetString(ByRef $aaArray, $asIndentChar="")
    Local $lsOutput=""
    Local $laCounters=[UBound($aaArray,0)] ; 1-based to avoid confusion
    Return __ArrayGetStringRecursed($aaArray, $lsOutput, $laCounters)
EndFunc

; Returns a string from an array of unknown dimensions, indenting the line farther for each dimension deep
; Uses an array of counters, one per dimension
; $asIndentChar could be a space or a tab to visually indent in addition to the numeric reference
Func __ArrayGetStringRecursed(ByRef $aaArray, ByRef $asIndentChar, ByRef $asOutput, ByRef $aaCounters, $anDimension=1)
    If Not IsArray($aaArray) Then Return SetError(1, 0, -1)
    
    If Not IsArray($aaCounters) Then
        ReDim $aaCounters[UBound($aaArray,0)+1] ; 1-based to avoid confusion
    EndIf
    
    ; Recurse until we're at the last dimension of the array
    If UBound($aaArray,0) > $anDimension Then
        $asOutput &= __ArrayGetStringRecursed($aaArray, $asOutput, $aaCounters, $anDimension+1)
    EndIf
    
; ==================================================
; This is where I'm getting hung up, accessing an array reference 
; built on the fly for an unknown amount of array dimensions
    
    For $aaCounters[$anDimension] = 0 to UBound($aaArray, $anDimension)
        Local $i, $lsArrayReference = "", $lsOutputReference=""
        For $i = 1 to UBound($aaArray,0)
            $lsArrayReference  &= "[" & $aaCounters[$i] & "]"
            $lsOutputReference &= $aaCounters[$i] & ","
        Next
        $lsOutputReference = StringTrimRight($lsOutputReference,1) ; Trailing comma
        
        ; We should end up with something like $lsArrayReference = "$aaArray[0][0][x]" with x incrementing on each loop
        ; First  run appends output from $lsArrayReference = "$aaArray[0][0][x]" for x from 0 to 2
        ; Second run appends output from $lsArrayReference = "$aaArray[0][1][x]" for x from 0 to 2
        ; Third  run appends output from $lsArrayReference = "$aaArray[0][2][x]" for x from 0 to 2
        ; Fourth run appends output from $lsArrayReference = "$aaArray[1][2][x]" for x from 0 to 2
        $asOutput &= $lsOutputReference & " = " & _
            __IndentString($anDimension, $asIndentChar) & _
            Eval("$aaArray" & $lsArrayReference) & @CRLF
    Next
    Return $asOutput & @CRLF ; Extra CRLF for a spacer
; ==================================================
EndFunc

Func __IndentString($anCount, $asChar=" ")
    Local $lsReturn=""
    While StringLen($lsReturn) < $anCount
        $lsReturn &= $asChar
    WEnd
    Return $lsReturn
EndFunc

3-dimensional array output:

0,0,0=1
0,0,1=  2
0,0,2=      3

0,1,0=4
0,1,1=  5
0,1,2=      6

0,2,0=7
0,2,1=  8
0,2,2=      9

1,0,0=10
1,0,1=  11
1,0,2=      12

1,1,0=13
1,1,1=  14
1,1,2=      15

1,2,0=16
1,2,1=  17
1,2,2=      18

2,0,0=19
2,0,1=  20
2,0,2=      21

2,1,0=22
2,1,1=  23
2,1,2=      24

2,2,0=25
2,2,1=  26
2,2,2=      27
Edited by c0deWorm

My UDFs: ExitCodes

Link to comment
Share on other sites

You should be able to achieve what you want by modifying this classical squeleton:

Global $gaTestArray[3][3][3][2] = [ _
    [ _
        [[0,1],[0,2],[0,3]], _
        [[0,4],[0,5],[0,6]], _
        [[0,7],[0,8],[0,9]]], _
        [[[0,10],[0,11],[0,12]], _
        [[0,13],[0,14],[0,15]], _
        [[0,16],[0,17],[0,18]]], _
        [[[0,19],[0,20],[0,21]], _
        [[0,22],[0,23],[0,24]], _
        [[0,25],[0,26],[0,27]] _
    ] _
]

ConsoleWrite(_VarDump($gaTestArray) & @LF)

Func _VarDump(ByRef $vVar, $sIndent = '')
    Select
        Case IsDllStruct($vVar)
            Return 'Struct(' & DllStructGetSize($vVar) & ') = ' & Hex($vVar)
        Case IsArray($vVar)
            Local $iSubscripts = UBound($vVar, 0)
            Local $sDims = 'Array'
            $iSubscripts -= 1
            For $i = 0 To $iSubscripts
                $sDims &= '[' & UBound($vVar, $i + 1) & ']'
            Next
            Return $sDims & @CRLF & _VarDumpArray($vVar, $sIndent)
        Case IsBinary($vVar)
            Return 'Binary(' & BinaryLen($vVar) & ')'
        Case IsBool($vVar)
            Return 'Boolean(' & $vVar & ')'
        Case IsFloat($vVar)
            Return 'Float(' & $vVar & ')'
        Case IsHWnd($vVar)
            Return 'HWnd(' & $vVar & ')'
        Case IsInt($vVar)
            Return 'Integer(' & $vVar & ')'
        Case IsKeyword($vVar)
            Return 'Keyword(' & $vVar & ')'
        Case IsPtr($vVar)
            Return 'Pointer(' & $vVar & ')'
        Case IsObj($vVar)
            Return 'Object(' & ObjName($vVar) & ')'
        Case IsString($vVar)
            Return 'String(' & StringLen($vVar) & ") '" & $vVar & "'"
        Case Else
            Return 'Unknown(' & $vVar & ')'
    EndSelect
EndFunc

Func _VarDumpArray(ByRef $aArray, $sIndent = '')
    Local $sDump
    Local $sArrayFetch, $sArrayRead, $bDone
    Local $iSubscripts = UBound($aArray, 0)
    Local $aUBounds[$iSubscripts]
    Local $aCounts[$iSubscripts]
    $iSubscripts -= 1
    For $i = 0 To $iSubscripts
        $aUBounds[$i] = UBound($aArray, $i + 1) - 1
        $aCounts[$i] = 0
    Next
    $sIndent &= @TAB
    While 1
        $bDone = True
        $sArrayFetch = ''
        For $i = 0 To $iSubscripts
            $sArrayFetch &= '[' & $aCounts[$i] & ']'
            If $aCounts[$i] < $aUBounds[$i] Then $bDone = False
        Next

        $sArrayRead = Execute('$aArray' & $sArrayFetch)
        If @error Then
            ExitLoop
        Else
            $sDump &= $sIndent & $sArrayFetch & ' => ' & _VarDump($sArrayRead, $sIndent)
            If Not $bDone Then
                $sDump &= @CRLF
            Else
                Return $sDump
            EndIf
        EndIf

        For $i = $iSubscripts To 0 Step -1
            $aCounts[$i] += 1
            If $aCounts[$i] > $aUBounds[$i] Then
                $aCounts[$i] = 0
            Else
                ExitLoop
            EndIf
        Next
    WEnd
EndFunc

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

$sArrayRead = Execute('$aArray' & $sArrayFetch)

Nice. So that's what I was missing. I suspected as much, but the help for Execute is somewhat vague.

Incidentally, is _VarDump in the standard UDFs shipped with AutoIt3? It's not in the help. Those might be useful permanent additions to the standard UDFs and help file, maybe in Misc.au3.

My UDFs: ExitCodes

Link to comment
Share on other sites

It's a classic found in the example scripts forum (search is your friend) but not in any std UDF. I believe I modified it long ago so the version(s) you're gonna find are probably slightly different, but only cosmetic as I recall.

It can save your day, eventually (I mean yours or mine, we all sometimes make severe blemishes).

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

For Au3Int I had a similar problem, with the addition that since the user could define whatever variables they like I needed to obfuscate the variable names... I made this function as a workaround. It uses a similar idea (based on execute) but assigns the variable name randomly, which means 1) Obfuscator doesn't mess up the name and 2) People won't accidentally assign a value to a global with the same name.

Local $a1[5] = [1, 2, 3, 4, 5]
Local $a2[5][2] = [[1, 2], [3, 4], [$a1, 46], [7, 8], [9, 10]]

_Au3Int_PrintVar($a2)

Func _Au3Int_PrintVar($vVar, $sPre = "")
    If IsArray($vVar) Then
        Local $sRandomVarName = Hex(Random(1000, 10000, 1))
        Assign($sRandomVarName, $vVar, 1)
        ConsoleWrite($sPre & "=> Array")
        For $i = 1 To UBound($vVar, 0)
            ConsoleWrite("[" & UBound($vVar, $i) & "]")
        Next
        ConsoleWrite(@CRLF)

        Local $a[UBound(Eval($sRandomVarName), 0) + 1], $iBnd = UBound(Eval($sRandomVarName), 0)
        For $i = 0 To $iBnd
            $a[$i] = 0
        Next

        $i = StringLen($sPre)
        $sPre = " "
        For $i = $i To -2 Step -1
            $sPre &= " "
        Next

        $a[$iBnd] = -1
        While 1
            $a[$iBnd] += 1
            For $i = $iBnd To 1 Step -1
                If $a[$i] = UBound(Eval($sRandomVarName), $i) Then
                    $a[$i] = 0
                    If $i = 1 Then ExitLoop 2
                    $a[$i - 1] += 1
                EndIf
            Next

            Local $sTmp = ""
            For $i = 1 To $iBnd
                $sTmp &= "[" & $a[$i] & "]"
            Next
            _Au3Int_PrintVar(Execute("$" & $sRandomVarName & $sTmp), $sPre & " " & $sTmp & " ")
        WEnd
        Return
    ElseIf IsString($vVar) Then
        $vVar = """" & $vVar & """"
    EndIf

    ConsoleWrite($sPre & "=> " & $vVar & @CRLF)
EndFunc   ;==>_Au3Int_PrintVar
Link to comment
Share on other sites

Correct!

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

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