Renderer

2d array iteration

6 posts in this topic

Hi guys ! How can i iterate a 2d array in AutoIt? Thanks in advance!

Share this post


Link to post
Share on other sites



#3 ·  Posted (edited)

When you say "iterate" you mean refer to the elements in a loop?  If so, its just rows and columns.  Use _arrayDisplay to visualize the data before you loop.  Here is an example: 

#include <Array.au3>

global $reproArray[5][2]=[["Bob",34],["James",22],["Sally",18],["David",43],["Cindy",28]]

;visualize data
_ArrayDisplay($reproArray)

; walk 2d array
for $a=0 to ubound($reproArray)-1
    ConsoleWrite("Name: " & $reproArray[$a][0]&" age: "&$reproArray[$a][1] & @crlf)
Next

 

Edited by Jfish

Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Share this post


Link to post
Share on other sites

@Jfish: I don't think that example is very helpful:o; not only is the column dimension wrong, the script is still treating the array as if it were 1D, because it does not loop through the columns.

Instead, I would suggest using nested for-next loops (one per dimension), like so:

Global $reproArray[6][2]=[["NAME","AGE"],["Bob",34],["James",22],["Sally",18],["David",43],["Cindy",28]]

; walk 2d array
For $rowindex=0 to Ubound($reproArray,1)-1
    For $colindex=0 to Ubound($reproArray,2)-1
        ConsoleWrite($reproArray[$rowindex][$colindex] &  @TAB)
    Next
    ConsoleWrite(@CRLF)
Next

This can easily be scaled up to handle any number of array dimensions.

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