Associtive Array for loop by index syntax help

Get help with using AutoHotkey and its commands and hotkeys
marzuk
Posts: 5
Joined: 26 Jun 2019, 09:45

Associtive Array for loop by index syntax help

02 Jul 2019, 18:12

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"]
}
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Associtive Array for loop by index syntax help

02 Jul 2019, 21:15

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"]

marzuk
Posts: 5
Joined: 26 Jun 2019, 09:45

Re: Associtive Array for loop by index syntax help

03 Jul 2019, 06:59

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?
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Associtive Array for loop by index syntax help

03 Jul 2019, 10:06

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.
just me
Posts: 7107
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Associtive Array for loop by index syntax help

03 Jul 2019, 10:29

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"]
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Associtive Array for loop by index syntax help

03 Jul 2019, 10:48

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
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
marzuk
Posts: 5
Joined: 26 Jun 2019, 09:45

Re: Associtive Array for loop by index syntax help

03 Jul 2019, 11:06

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.
Helgef
Posts: 4409
Joined: 17 Jul 2016, 01:02
Contact:

Re: Associtive Array for loop by index syntax help

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.
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Associtive Array for loop by index syntax help

03 Jul 2019, 12:42

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.
Helgef
Posts: 4409
Joined: 17 Jul 2016, 01:02
Contact:

Re: Associtive Array for loop by index syntax help

03 Jul 2019, 12:50

Hi wolf_II :wave:.
I guess that's right.

Cheers.
just me
Posts: 7107
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Associtive Array for loop by index syntax help

03 Jul 2019, 16:47

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:
trust_me
Posts: 83
Joined: 29 Jul 2017, 10:46

Re: Associtive Array for loop by index syntax help

04 Jul 2019, 09:08

:offtopic: just me ! now the song sticks in my head like chewing gum to a shoe............... :headwall:

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], colt, HooptyDoopty, Ian, smogmanus1, SpikeVanHellsing, Tamer and 296 guests