Jump to content
Sign in to follow this  
snomy

The old 2d Array chestnut

Recommended Posts

snomy

hi

lets say i want to create a 2d array, such as this:

test1, apple

test2, banana

test3, cherry

i know the array size beforehand is [2][3].

i want to then extract each row item and print it to the screen in a msgbox say....

1st time: test1

2nd time: apple

3rd time: test2

4th: banana

etc etc.....

so i need two bits of help here, one is how to create the array, which i have done here, please check and comment:

Dim $arGrid[$i][$j] = [["test1", "test2", "test3"], ["apple", "banana", "cherry"]]

then i need the while loop (i presume to extract in the method as above)

can you help? or point me to some good information to understand the 2d array? thanks!!!!!!!!!!!!

Share this post


Link to post
Share on other sites
SadBunny

I wrote a quick example for you:

Dim $arGrid[2][3] = [["test1", "test2", "test3"], ["apple", "banana", "cherry"]]

; show all columns per row:
For $x = 0 To UBound($arGrid,1)-1; <-- dimension 1
    For $y = 0 To UBound($arGrid,2)-1; <-- dimension 2
        MsgBox(0,"Array element "&$x&","&$y,$arGrid[$x][$y])
    Next
Next

; show all rows per column:
For $y = 0 To UBound($arGrid,2)-1; <-- dimension 2
    For $x = 0 To UBound($arGrid,1)-1; <-- dimension 1
        MsgBox(0,"Array element "&$x&","&$y,$arGrid[$x][$y])
    Next
Next
/edit: fixed dimension comments in second part :">

Note the difference between the two parts. The latter seems to be what you mean, though reorganizing your array in 3x2 and using the first type seems more intuitive for your example.

Your example:

test1 - test2 - test3

apple - banana - cherry

My suggestion:

test1 - apple

test2 - banana

test3 - cherry

But this ofcourse all depends on the actual data you were going to organize... :P

Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites
BrettF

Edited you example... :P

Dim $arGrid[2][3] = [["test1", "test2", "test3"], ["apple", "banana", "cherry"]]
$msg = ""
; show all rows per column:
For $y = 0 To UBound($arGrid,2)-1; <-- dimension 2
    For $x = 0 To UBound($arGrid,1)-1; <-- dimension 1
        $msg &= "["&$x&"]["&$y&"] = " & $arGrid[$x][$y] & @CRLF
    Next
Next
MsgBox (0, "", $msg)

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
Sign in to follow this  

×