Sign in to follow this  
Followers 0
LWC

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

11 posts in this topic

#1 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

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.  

Share this post


Link to post
Share on other sites

;some code

But that's exactly the missing part. How can I display or edit what's inside $array[$row][$column], which won't work because they're numbers.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

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.  

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites

How are you creating the array? It's not build-in, or even a standard UDF.

The best way to figure you how to get data from a source is finding out how it was put in there in the first place.

Share this post


Link to post
Share on other sites

As @Tvern said, AutoIt does not natively support the types of arrays you are referencing.

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

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

Share this post


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

Share this post


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

Share this post


Link to post
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

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  
Followers 0