Page 1 of 1

Associtive Array for loop by index syntax help

Posted: 02 Jul 2019, 18:12
by marzuk
I have a nested associative array that I'm populating from an ini in a loop.

it mirrors the structure below. In my use case the "sections" are not known at runtime, nor how many books in a section.

I need to be able to loop through the books in a given section sequentially and access a book by index arbitrarily.

I could use a loop until an index that does not exist, use loop, books["section"].Length() and A_Index

both of those seem like I'm missing some thing obvious, where a for loop seems like the natural way to do what I need, but I'm pushing new objects into an object so they have no associative key.

It might just be the case that for loops are semantically looping through keys and I dont have an "each" and I'm connecting dots that dont connect.

So, do I skip the For loop here and foll my own loop?

Code: Select all

books := Object()

books["fiction"] := Object()

loop, 5
{
book := Object()
book["title"] := "book" . A_Index
books["fiction"].push(book)
}
loop, 5
{
msgbox % books["fiction"][A_Index]["title"]
}

Re: Associtive Array for loop by index syntax help

Posted: 02 Jul 2019, 21:15
by wolf_II

Code: Select all

books["fiction"] := Object()
this can/should be changed to

Code: Select all

books["fiction"] := Array()
b/c under the hood, AHK v1.1 treats them equally, you get away with either.

Example:

Code: Select all

books := Object()

books["fiction"] := Array()

loop, 5
{
book := Object()
book["title"] := "book" . A_Index
books["fiction"].push(book)
}

for each, book in books["fiction"]
    msgbox % book["title"]


Re: Associtive Array for loop by index syntax help

Posted: 03 Jul 2019, 06:59
by marzuk
You say I should use Array instead of Object, then say they are the same under the hood.

If thats the case what makes that a should?

Re: Associtive Array for loop by index syntax help

Posted: 03 Jul 2019, 10:06
by wolf_II
You say I should use Array instead of Object, then say they are the same under the hood.
If thats the case what makes that a should?
Should is to help with consistency/clarity.
Your code is only using books["fiction"] as an simple array. not as a associative array.

Here: "I have a nested associative array that I'm populating from an ini in a loop."
No, You don't. What you have is an inner ass.array, inside a simple array, in a second ass.array.

Writing code clearer would help. That's the only reason, why I used should. AHK handles it either way.

Re: Associtive Array for loop by index syntax help

Posted: 03 Jul 2019, 10:29
by just me
AHK objects are associative arrays and vice versa. If you want to create an empty array/object

Code: Select all

Var := Array()
Var := Object()
Var := [] ; Array()
Var := {} ; Object()
create identical structures in memory.

Code: Select all

Var := ["One", "Two", "Three"] ; 'array' syntax
and

Code: Select all

Var := {1: "One", 2: "Two", 3: "Three"} ; 'object' syntax
create identical structures in memory also. Both are associative arrays. And both have only numeric keys associated with plain values.

In this case Books is an associative array with section names used as string keys. Each string key of the first dimension is associated with a 'simple' array using numeric keys or indices. Each numeric key (index) of the second dimension is associated with an associative array of string keys like Title associated with plain values.

So that's one way how I would use it:

Code: Select all

Books := {}
Books["Fiction"] := []
Loop, 5
{
   Book := {}
   Book["Title"] := "Book" . A_Index
   Books["Fiction"].Push(Book)
}
For Index, Book In Books["Fiction"]
   MsgBox, 0, Book # %Index% in Fiction, % Book["Title"]

Re: Associtive Array for loop by index syntax help

Posted: 03 Jul 2019, 10:48
by jeeswg
AHK v2 may distinguish between arrays (linear arrays) and maps (associative arrays) in future. And so, []/Array and {}/Object, would no longer be equivalent.

Objects - preview of upcoming changes - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=37&t=57591

Re: Associtive Array for loop by index syntax help

Posted: 03 Jul 2019, 11:06
by marzuk
Thanks all! responses were helpful because they caused me to reevaluate a few things I was doing. All I had to do was make a few tweaks to get things moving in the right direction.

Re: Associtive Array for loop by index syntax help

Posted: 03 Jul 2019, 12:18
by Helgef
The documentation recommends,
objects wrote:In AutoHotkey v1.x, simple arrays and associative arrays are the same thing. However, treating [] as a simple linear array helps to keep its role clear, and improves the chance of your script working with a future version of AutoHotkey, which might differentiate between simple arrays and associative arrays.

Re: Associtive Array for loop by index syntax help

Posted: 03 Jul 2019, 12:42
by wolf_II
Hi, Helgef :wave:
In the post above, would I be right to limit all x to 1 (or higher)?
eg. AHK 1.0 has no associative arrays.

Re: Associtive Array for loop by index syntax help

Posted: 03 Jul 2019, 12:50
by Helgef
Hi wolf_II :wave:.
I guess that's right.

Cheers.

Re: Associtive Array for loop by index syntax help

Posted: 03 Jul 2019, 16:47
by just me
Helgef wrote:
03 Jul 2019, 12:18
The documentation recommends,
objects wrote:In AutoHotkey v1.x, simple arrays and associative arrays are the same thing. However, treating [] as a simple linear array helps to keep its role clear, and improves the chance of your script working with a future version of AutoHotkey, which might differentiate between simple arrays and associative arrays.
In the year 2525 ... :wave:

Re: Associtive Array for loop by index syntax help

Posted: 04 Jul 2019, 09:08
by trust_me
:offtopic: just me ! now the song sticks in my head like chewing gum to a shoe............... :headwall: