Page 1 of 1

Multi-level associative arrays

Posted: 26 May 2014, 18:42
by TAC109
I'm trying to get to grips with multi-level associative arrays.

I find the documentation to be very brief and lacking useful examples.

How do I define an empty multi-level associative array?

How do I use the 'for' command to extract values from a multi-level associative array?

Can I enumerate the key:values in reverse order?

(I believe I understand how to insert key:value pairs into a multi-level associative array.)

Thanks for any help.

Re: Multi-level associative arrays

Posted: 26 May 2014, 21:20
by joedf
All arrays in autohotkey are technically "associative arrays". Is your question about the for loop in reverse and/or an array declaration?
arr := { keyname1: var1, key2: valueB }
arr := { 1: blah, 2: asdf }
Is the order really important? Is it possible to store the information in reverse? Are you using numbers as Keys? If so, what have you tried?

Re: Multi-level associative arrays

Posted: 26 May 2014, 22:26
by TAC109
Sorry if I'm no being clear. I understand associative arrays.

I've got 3 questions:-

1. How do I define an empty multi-level associative array?

2. How do I use the 'for' command to extract values from a multi-level associative array?

3. Can I enumerate the key:values in reverse order?

The first two relate to multi-level associative arrays and the last is more general.

Thanks

Re: Multi-level associative arrays

Posted: 26 May 2014, 22:50
by joedf
1. Ok, since associative arrays must have keys, this would be the closest...
An example of an "empty" 3x3 array:

Code: [Select all] [Download] (Script01.ahk)GeSHi © Codebox Plus

MySubArray_1 := { 1:{ }, 2:{ }, 3:{ } }
MySubArray_2 := { 1:{ }, 2:{ }, 3:{ } }
MySubArray_3 := { 1:{ }, 2:{ }, 3:{ } }
MyArray := { 1:MySubArray_1, 2:MySubArray_2, 3:MySubArray_3 }

2. not sure, but here is an example:

Code: [Select all] [Download] (Script02.ahk)GeSHi © Codebox Plus

MySubArray_1 := { 1:"A", 2:"B", 3:"C" }
MySubArray_2 := { 1:"D", 2:"E", 3:"F" }
MySubArray_3 := { 1:"G", 2:"H", 3:"I" }
MyArray := { 1:MySubArray_1, 2:MySubArray_2, 3:MySubArray_3 }

for every, SubArray in MyArray
for each, item in SubArray
MsgBox SubArray: %every%`nValue [%each%]: %item%

3. Well, in reverse could be done without much trouble if the arrays have numbers as keys...
An example: I made a neat little recursive Array-reverse function there ;)

Code: [Select all] [Expand] [Download] (Script03.ahk)GeSHi © Codebox Plus



Cheers, hope this helps :)

Re: Multi-level associative arrays

Posted: 27 May 2014, 02:19
by LinearSpoon
2. More generally, you simply check if each item you come to is a subarray, and if so go through a for loop on the subarray. This is one of the few algorithms it feels natural to write recursively. This serialize function is something I wrote a while back to quickly print out the contents of most objects. It might give you some ideas.

Code: [Select all] [Expand] [Download] (Script.ahk)GeSHi © Codebox Plus



3. You could write a custom enumerator. This allows you to continue using for-loop syntax, and preserves the original object.

Code: [Select all] [Expand] [Download] (Script.ahk)GeSHi © Codebox Plus


As a side note, I find this to be misleading: for each, item in SubArray
It's nice that it reads as an English phrase, but someone not familiar with the syntax may wonder why there is a comma when it isn't appropriate grammatically, and it gives no indication that each is actually a variable name.

Re: Multi-level associative arrays

Posted: 27 May 2014, 08:55
by guest3456
joedf wrote:1. Ok, since associative arrays must have keys, this would be the closest...
An example of an "empty" 3x3 array:

Code: [Select all] [Download] (Script01.ahk)GeSHi © Codebox Plus

MySubArray_1 := { 1:{ }, 2:{ }, 3:{ } }
MySubArray_2 := { 1:{ }, 2:{ }, 3:{ } }
MySubArray_3 := { 1:{ }, 2:{ }, 3:{ } }
MyArray := { 1:MySubArray_1, 2:MySubArray_2, 3:MySubArray_3 }


this is actually 3 levels deep and is a 3x3x1 array

Re: Multi-level associative arrays

Posted: 27 May 2014, 08:57
by joedf
Haha you're right :P

Re: Multi-level associative arrays

Posted: 27 May 2014, 17:34
by TAC109
Thanks for the help everyone.

@joedf: thanks for your code that shows the basics.

@LinearSpoon: thanks for your example code, showing that the multi-level arrays can be unbalanced. As I want my object to reflect a folder with files and sub-folders, etc this will be very useful.

Re: Multi-level associative arrays

Posted: 27 May 2014, 17:39
by joedf
No problemo! ;)