Jump to content

For-in loop through multidim array


Recommended Posts

Hello, I try to use this:

Dim $aArray[1][4] = [[0, 1, 2, 3]]

FOR $element IN $aArray[0]
    MsgBox(0, "test", $element )
NEXT

But I get error:

Array variable has incorrect number of subscripts or subscript dimension range exceeded.: 
FOR $element IN $aArray[0] 
FOR $element IN ^ ERROR

AutoIt Version 3.2.2.0

If the array was one-dimensional, the problem wouldn't occur.

Edited by amokoura
Link to comment
Share on other sites

Hello, I try to use this:
Dim $aArray[1][4] = [[0, 1, 2, 3]]

FOR $element IN $aArray[0]
    MsgBox(0, "test", $element )
NEXToÝ÷ Ø­"­zºè­Êx
ëk+Ú®&èZ²)Ü¢ºÞrÙî·«¡û.nÇ+l¢».nÇ+]g§²*'­©àyìyç^w97éé^éí Ý7é +­¬½×Owätߧ¥zg§´DEýÊx­ Uz»"¢}öÛB¶®¶²Á«(çbéìÚØ^¦ºé°¢é]m¡Ç.¯ú®¢×¢ë-YgyçmërÝv)ȨÊ-áNFX§zZ(Ë(ëax%GºÚ"µÍÜ   ÌÍÙ[[Y[[ ÌÍØP^VÌWVÍ

See if that works and tell us how you go. :shocked:

Link to comment
Share on other sites

For $element In $aArray[1][4]oÝ÷ Ûú®¢×©ä±»­ËbX(¶Ø^±©zºè®È³z쥩ì·)^

Error:

E:\AUTOIT\test.au3 (8) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.: 
$aTest = $aArray[0] 
$aTest = ^ ERROR

I can't copy the [0,1,2,3]-array to $aTest.

Edited by amokoura
Link to comment
Share on other sites

Why are you using a 2D array with only one element in the first dimension? Shouldn't that be:

Dim $aArray[4] = [0, 1, 2, 3]

FOR $element IN $aArray
    MsgBox(0, "test", $element )
NEXToÝ÷ Ù8b²+²Ö§uªÝ)Â+ak`Ú®¶²jëh×6$aTest = ""
Dim $aArray[2][4] = [[0, 10, 20, 30], [5, 15, 25, 35]]
For $a = 0 To 1
    For $b = 0 To 3
        $aTest &= "[" & $a & "][" & $b & "] = " & $aArray[$a][$b] & @LF
    Next
Next
MsgBox(64, "2D Array", $aTest)

It doesn't look like For/In/Next accepts a 2D array. More than one dimension requires more than one index, and For/In/Next only creates a single index - so it can only use 1D arrays. At least, that's how it looks to me...

:shocked:

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

....It doesn't look like For/In/Next accepts a 2D array....

Yes that's how it looks.

But still, how can I extract an array from a 2D-array? What's the problem here:

$aTest = ""

Dim $aArray[1][4] = [[0, 1, 2, 3]]

$aTest = $aArray[0]
Link to comment
Share on other sites

Yes that's how it looks.

But still, how can I extract an array from a 2D-array? What's the problem here:

$aTest = ""

Dim $aArray[1][4] = [[0, 1, 2, 3]]

$aTest = $aArray[0]

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Link to comment
Share on other sites

Dim $aArray[1][4] = [[0, 1, 2, 3]]

For $x = 0 To UBound($aArray) - 1
    For $y = 0 To UBound($aArray,2) - 1
        MsgBox(0,"Test",$aArray[$x][$y],10)
    Next
Next
Thanks for the workaround. Was there some bad code in my example or should I create a bug report?

From the manual:

A unique feature in AutoIt is the ability to copy arrays like this:

$mycopy = $myarray

Edited by amokoura
Link to comment
Share on other sites

Thanks for the workaround. Was there some bad code in my example or should I create a bug report?

Your code was bad because you tried to use the wrong number of indexes. You provided only one subscript (or index) when the array needed 2:

$aTest = $aArray[0]oÝ÷ Ù(hºW[+{¦¦WºÚ"µÍÌÍØUÝH  ÌÍØP^VÌVÉÌÍØW

Since For/In/Next will only create a single variable, you can't do that with multi-dimensional arrays. Just use nested For/Next like gafrost and I both gave examples of.

:shocked:

P.S. This does not answer, however, why you wouldn't just use a 1D array instead of a 2D array with only one subscript in the first dimension?

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

Your code was bad because you tried to use the wrong number of indexes. You provided only one subscript (or index) when the array needed 2:

$aTest = $aArray[0]oÝ÷ Ù(hºW[+{¦¦WºÚ"µÍÌÍØUÝH  ÌÍØP^VÌVÉÌÍØWoÝ÷ Øô+¡ë'¢Ö§³«½êð,¨»
.Ù÷öØî²Û¬y­CjºÚÊ)ìµæ¡ö¶
ªëk,"'*'z˱Êâ¦Ø§¶»-v)Ȩú®¢×v÷öÜÛh{kiËZ²)àëÚ箴ߦ®¶²#§¶Ø^jºÚÊ)ì×¢·Múh
ëk-æâÇ+HßÙ­æ­x.±ë6
ªëk+×±¶¶¶)ඪºbw«ªê-jºÚÊ+,¢g­g¬ºÇºQÞ­íý±©è¶«{¦¦W¨}©Ú®¶²rrx¶×¦¦Ö®¶­sdFÒb33c¶'&ôg'VG5³%Õ³%ÒÒ²²gV÷C¶&æægV÷C²ÂgV÷C·VÆÆ÷rgV÷CµÒ²gV÷C¶ÆRgV÷C²ÂgV÷C¶w&VVâgV÷CµÒТb33c¶'&ôÆRÒb33c¶'&ôg'VG5³Ð
Same error still...
Link to comment
Share on other sites

Here's another example of an array copying attempt:
Dim $array_Fruits[2][2] = [ ["banana", "yellow"], ["apple", "green"] ]
$array_Apple = $array_Fruits[1]oÝ÷ Ù&¦yê뢻-Yªê-y8^³)íkî²×hzÉ÷öÜ(®KajÜÊ)ÀºÚ´­êÞßÛ­ m®'$²v÷öÙ'£áyÊ'µéí²Úªëk'¥zg§µÆ§m槢Ø^­éí·®¶²jëh×6Dim $avEven[5] = [0, 2, 4, 6, 8]
Dim $avOdd[5] = [1, 3, 5, 7, 9]

Dim $avDigits[2] = [$avEven, $avOdd]
$Test = $avDigits[0]

$Msg = ""
For $d = 0 To UBound($Test) - 1
    $Msg &= $Test[$d] & @LF
Next
ConsoleWrite("Even digits:" & $Msg)oÝ÷ ØË¥¶'béìÚªëk+jw(Ö¢k­'béìÚªëk+±é^éí³}Î}Ê.®Ç­+*º'$&Þr²Ê&w[¢%x¢je{§vÉhÂÚÅç.µëajÙè®f¥¥ê)毢'b´
Þʧ¢¹Ò!ü¨»§¶Ú.±æ§jºÚÊØZ¶+¢{Zwv§¶«jºÚË*.«Þ¶(§(­¢ë_»-"{ay¦è½ë®*m×(¦'¢ëm£MúMë-o&®¶­sbb33cµFW7BÒb33c¶dFvG5³oÝ÷ Ù8b±ú+{¦¦W¡ë'¢Ü(®F®¶­sdFÒb33c¶dWfVå³UÒÒ³Â"ÂBÂbÂФFÒb33c¶döFE³UÒÒ³Â2ÂRÂrÂÐ ¤FÒb33c¶dFvG5³%ÒÒ²b33c¶dWfVâÂb33c¶döFEÐ ¢b33c´×6rÒgV÷C²gV÷C°¤f÷"b33c¶BÒFòT&÷VæBb33c¶dFvG5³ÒÒ b33c´×6rf׳Òb33c¶dFvG5³Õ²b33c¶EÒfײÄ`¤æW@¤6öç6öÆUw&FRgV÷C´öFBFvG3¢gV÷C²fײb33c´×6r

:shocked:

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

The syntax just doesn't work that way in AutoIt.
Pity :P

But, here's an AutoIt trick some don't know. The contents of an array element can be another entire array:

Dim $avEven[5] = [0, 2, 4, 6, 8]
Dim $avOdd[5] = [1, 3, 5, 7, 9]
Dim $avDigits[2] = [$avEven, $avOdd]
I though $avEven and [2,4,6] are both arrays, exactly the same??

Sometimes :(

OK. Thank you very much for the help!
Link to comment
Share on other sites

That really boggles my mind:

Dim $avDigits[2] = [$avEven, $avOdd] ;works
Dim $avDigits[2] = [[2,4,6],[1,3,5]] ;doesn't work for sub-array copyingoÝ÷ Øa¢è!Ó~¼KÞ©ÝÛ­æ趫­¬¬{¶­ëïêº^"y²{¦¦W´ß¦¯÷§Ó~¼ç]jwtߦ¯("¶Æ«y©ejºÚÊÍ7é«Ã­²+Ô6«­¬°Øh­Â¥zg§¶ÍÝH±ëmç(ק¶Ê¶,zÜ(zW¦z{l¶§jºÚËbr¬º{¬¹©ZÙ趷¢i×N­Æ¢¨~Ø^jºÚÊÍ7é«Ä½éÚÝ7é«ÎuÔáyé^éí²í²Ú+yÖ«­¬¬q©ç¢ÖÞi×kzËw­­~*ìµÊ)Ê)ඦ¢ëm¡¬(®H§ö«¦å{MúMë-y²{¦¦WyÊ&zƵ
ªëk"'Ê«x­©bÍ«b¢zÓ~¼8 ÛnÞÊ©®âuè§Ø,Ú¶*'~íÂbéììnëmæ«­¬¨¡jÊ'yئz{"¢|"¶p¡é^éí±8^rëyË_¢¹¨~ØZ·
.ØZ½æÞzv®¶­sdFÒb33c¶dFvG5³%Õ³5ÒÒµ³"ÂBÂeÒ³Â2ÂUÕÐoÝ÷ Ù.Â¥vØ^¯zÚqǬ²+)à·
"×±zÆ®¶­sbb33c´çVÒÒb33c¶dFvG5³Õ³%Ò²&VG2b33³bb33°

:shocked:

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

you can use my _ArrayDisplay2() function if you need to look at your array if it helps, it can display 1d or 2d arrays

Func _ArrayDisplay2(Const ByRef $avArray, $sTitle = "")
    
    If (Not IsArray($avArray)) Then
        SetError(1)
        Return 0
    EndIf
    
    #include<guiConstants.au3>
    Local $iRows = 0, $iCols = 0, $cHeaders, $iCnt = 0, $lv ,$lCnt = 0, $tCnt = 0, $rtext, $cButton,$lvGui,$lvGuiMsg

    $iRows = Ubound($avArray) -1
    $icols = Ubound($avArray,2)
    
    $cHeaders = "I/C |0|"
    
    For $iCnt = 1 to $iCols -1
        $cHeaders &= $iCnt & "|"
    Next
    
    $lvGui = GuiCreate($sTitle,400,300)
    $lv= GuiCtrlCreateListview($cHeaders,20,20,360,220)
    
If $icols <> 0 then 
    
    For $lCnt = 0 to $iRows
        $rtext = "[" & $lCnt &"]|"
        For $tCnt = 0 to $icols -1
            $rText &= $avArray[ $lcnt][$tcnt] & "|"
        Next
        GUICtrlCreateListViewItem($rText,$lv)
    Next
    
Else
    
    For $lCnt = 0 to $iRows
        $rtext = "[" & $lCnt &"]|" & $avArray[$lCnt]
        GUICtrlCreateListViewItem($rText,$lv)
    Next
    
    
EndIf


$cButton = GuiCtrlCreateButton("Close", 150, 260, 110, 30)


GuiSetState()

While 1
    $lvGuiMsg = GuiGetMsg(1)
    Select
        Case $lvGuiMsg[0] = $GUI_EVENT_CLOSE or $lvGuiMsg[0] = $cButton
        If $lvGuiMsg[1] = $lvGui then ExitLoop
        
        Case Else
        ;;;
    EndSelect
WEnd
    
    SetError(0)
    Return 1

EndFunc
Link to comment
Share on other sites

For..In..Next loops are only to be used with objects!. For..Next loops are useful for Arrays.

Not true. It works with collection objects because they are arrays!

Dim $avDigits[10] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
For $d In $avDigits
    ConsoleWrite($d & @LF)
Next

:shocked:

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

...

In your initialization of $avDigits above you provide initialization for two dimensions but the array only has one dimension with two elements.

...

OK I can see clearly now, thanks! :shocked:

I'm a child of PHP and it tought me array handling. I recall it had a more flexible array handling system regarding to dimensions and elements. I mistakenly expected AutoIt arrays to work the same way. Or do I remember wrong? Any PHP guys?

you can use my _ArrayDisplay2() function if you need to look at your array if it helps, it can display 1d or 2d arrays

Thanks a lot! Will come in handy.
Link to comment
Share on other sites

OK I can see clearly now, thanks! :shocked:

I'm a child of PHP and it tought me array handling. I recall it had a more flexible array handling system regarding to dimensions and elements. I mistakenly expected AutoIt arrays to work the same way. Or do I remember wrong? Any PHP guys?

Thanks a lot! Will come in handy.

I had a feeling you might have come from PHP, I did also and was also confused by the way AutoIt handled their multi-dimensional arrays. As you know, in PHP a multi-dimensional array is actually an array within an array, and you can access it as such. In AutoIt it doesn't behave that way, it acts ONE array, with multiple dimensions, and you have to access it from the base of the entire array... I have a hard time explaining it, since as near as I know, PHP can't behave the same way at all. I'll try to describe it as such.

With PHP, you can do something like this:

$myarray = array(1, 2, 3, 4, array(1, 2, 3, 4));
print_r($myarray);

And you get something like this:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
        )

)

Notice how it says that index [4] of the first array IS an array?

Now, if you could use print_r() on this array (an example I saw previously), you'd perhaps get something like the following:

; Array example
Dim $array_Fruits[2][2] = [ ["banana", "yellow"], ["apple", "green"] ]

; and example print_r output
Array
(
    [0,0] => banana
    [0,1] => yellow
    [1,0] => apple
    [1,1] => green
)

Hmm.. bah, I don't know how well that works. I'm really not good at this explaining thing. Hopefully this doesn't create more confused than before.

Link to comment
Share on other sites

...

In AutoIt it doesn't behave that way, it acts ONE array, with multiple dimensions, and you have to access it from the base of the entire array... I have a hard time explaining it, since as near as I know, PHP can't behave the same way at all.

...

OK thanks, that finally verifies my thoughts.

I :shocked: PHP arrays. In the old days I built houses by using them.

Edited by amokoura
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...