Jump to content

2d array iteration


Renderer
 Share

Recommended Posts

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

Link to comment
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.

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