Jump to content

How can one loop through a multi-dimentional associative array?


LWC
 Share

Recommended Posts

For...In...Next:

If the expression is a multi-dimensional array, the loop will be skipped and the Variable will contain an empty string.

So how is one supposed to loop through a multi-dimentional associative array ($array["foo"]["bar"])?

Thanks!

Edited by LWC
Link to comment
Share on other sites

For $row=0 To UBound($array,1)-1
    For $column=0 to UBound($array,2)-1
        ;some code
    Next
Next

Edited by Realm

My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry. 

Link to comment
Share on other sites

For $r=0 To UBound($array,1)-1
    For $c=0 to UBound($array,2)-1
        MsgBox(0,"",$array[$row][$column]) ;Will display a Message Box with that Array's Cell content
    Next
Next

It would be alot easier to help with your problem if you post some code, and tell us what your trying to achieve, at this point, trying to guess what you need is as easy as picking the correct number out of 10 billion

to edit a given cell try

$array[$row][$column] +=1

or

$text="New Name"
$array[$row][$column] =$text
Edited by Realm

My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry. 

Link to comment
Share on other sites

Again, I'm talking about associative arrays. For example:

$array["foo"]["bar"]="bla"
$array["foo"]["test"]="something"

Your loop code won't work with associative arrays. How can I iterate through multi-dimentional associative arrays?

Link to comment
Share on other sites

I'm creating it manually:

dim $array[1][2]
$array["foo"]["bar"]="bla"
$array["foo"]["test"]="something"

Now I just to loop through it. AutoIt supports such arrays. For example, I can do:

msgbox(0, "", $array["foo"]["bar"])

I just need to know how to loop through it.

Hmm, it seems "test" overwrites "bar".

Edited by LWC
Link to comment
Share on other sites

I am sorry, I overlooked the word associative, which is a bit over my head, but I did find a couple items of interest in the forums, there is a UDF that deals with multidimensional associative arrays here http://www.autoitscript.com/forum/index.php?showtopic=113182&st=0

Also another that has a function,_Key_GetKeys(ByRef $kArray) , to get an array of keys, which might help in looping and is found here http://www.autoitscript.com/forum/index.php?showtopic=93489&st=0&p=671577&hl=associative%20array&fromsearch=1&#entry671577

My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry. 

Link to comment
Share on other sites

That's not an associative array. Autoit doesn't have them natively.

Instead all strings (in this case) are evaluated as "0", so you're just writing values to $array[0][0] all the time.

If you want to use associative arrays you'll have to search for a UDF that you like, or write one yourself.

Link to comment
Share on other sites

I'm creating it manually:

dim $array[1][2]
$array["foo"]["bar"]="bla"
$array["foo"]["test"]="something"

Now I just to loop through it. AutoIt supports such arrays. For example, I can do:

msgbox(0, "", $array["foo"]["bar"])

I just need to know how to loop through it.

Hmm, it seems "test" overwrites "bar".

As Tvern said, you are faking yourself out. AutoIt does not support associative arrays natively. AutoIt variables are also not strictly typed, so when you pass a string where an integer is required for the index, there is an attempt to convert it silently as if by $array[int("foo")][int("bar")], which evaluates to $array[0][0]. This shows the actual results of your code:
#include <Array.au3> ; only for _ArrayDisplay()

dim $array[1][2]

$array["foo"]["bar"]="bla"
_ArrayDisplay($array, "$array")

$array["foo"]["test"]="something"
_ArrayDisplay($array, "$array")

;)

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

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