Page 1 of 1

Looping, scrolling or browsing through associative arrays

Posted: 03 Jun 2018, 02:40
by JemyM
Based on this thread I finally moved over to what I believe is called "associative arrays" in my work. But I ran into an obstacle. I have a lot of things I want to do that involves browsing through the array, like displaying the array in a listview, appending specific values into text files, do the same operation to all or a given set of keys, go to the "next" or "previous" key in a GUI etc.

But since the key here is an ID number, its not as easy as to simply rely on IDIndex[1] or IDIndex[A_Index] anymore.

How can I access say the #550 key in the Array, or flip between #1100-#1110 without confusing it with the actual keylabels themselves that consists of 7000 ID numbers that may span between 1 to 100000?

Re: Looping, scrolling or browsing through associative arrays

Posted: 03 Jun 2018, 02:57
by JemyM
I realized that this is exactly the same problem I have with XML. Am I think about this wrong? For me it makes sense that databases may be numbered and browsed through, both Associative Arrays and XML.

Re: Looping, scrolling or browsing through associative arrays  Topic is solved

Posted: 03 Jun 2018, 04:54
by swagfag
associative arrays are unordered(well, they are, numerically and then alphabetically, but for all intents and purposes they arent), so you have 2 options, really:
1. shove your "Entries" as objects in a regular array. this will complicate looking up by 'MobyID' for example, as youll have to iterate over the whole array, doing comparisons
2. keep using your
Associative Array
, insert a key marking its intended position in the array when creating the array. this will preserve the option to lookup by 'MobyID', but, you guessed it, if u want to lookup by the index youll have to loop over the entire array and do some checking

it is evident, from your previous threads even, that u need something more flexible and going either of the routes ive outlined above is setting yourself up for a world of hurt
i lied when i said there were 2 option, there are in fact 3. the third one being: https://autohotkey.com/boards/viewtopic ... 67#p221067

Re: Looping, scrolling or browsing through associative arrays

Posted: 03 Jun 2018, 05:42
by JemyM
swagfag wrote:associative arrays are unordered
Thanks again.

I will use "1". I can actually use that to access the XML too at this point. I was just hoping it could be done without an additional array.

The MobyID is one of 2 core elements that fuse 10 separate sources together and I have a master index for that purpose. I can simply read that one into an array too.

The purpose of using ahk is to fuse different sources. I have 11 sources with 5 variants, and I try to make them all readable using the same commands. The other types are sets of .ini files, a custom dat file, 2 file libraries of 25k zips and an xml file.

Re: Looping, scrolling or browsing through associative arrays

Posted: 03 Jun 2018, 06:08
by SirRFI
Difference between indexed-array ([]) and associated-array ({}) is that the first one sets integer key automatically to reflect "position", while in the second key can be string and has to be set manually. Since the keys are no longer numbers, you can't simply use Loop % array.length() to loop through it. However, there's a better way to go through both of them: for-loop. It will go through every array element on this level/depth, without even using A_Index. You have to remember it will go through keys in ascending order, no matter in which they were declared:

Code: Select all

for key, value in { "b": "qwe", "a": "asd", "c": "zxc" }
{
	MsgBox % "Key '" key "' has value: " value
}

Re: Looping, scrolling or browsing through associative arrays

Posted: 03 Jun 2018, 06:51
by JemyM
SirRFI wrote:However, there's a better way to go through both of them: for-loop. It will go through every array element on this level/depth, without even using A_Index.
For loop will be useful for a lot of purposes actually.

I tried this... IDIndex is an array of identification numbers called MobyID.

Code: Select all

for key, value in IDIndex
MsgBox % "MobyID '" key " have the value " value
But the value end up blank. Each key in IDIndex also has its own subarray documenting among other things an executeable file. I can currently learn the Executeable associated with the Key by using IDIndex[69883].Executeable

Do you know how complete the above piece of code so that "value" becomes whatever is stored in the Executeable section in the subarray?

Re: Looping, scrolling or browsing through associative arrays

Posted: 03 Jun 2018, 07:01
by SirRFI
JemyM wrote:But the value end up blank. Each key in IDIndex also has its own subarray documenting among other things an executeable file. I can currently learn the Executeable associated with the Key by using IDIndex[69883].Executeable

Do you know how complete the above piece of code so that "value" becomes whatever is stored in the Executeable section in the subarray?
Change IDIndex[69883].Executeable to value.Executeable.
Value contains the contents, so if the contents is another array/object/whatever, you have to access it like you normally would. Think of value as IDIndex[69883] for the moment being.
Of course you can rename key and value variables to suit your need.

Re: Looping, scrolling or browsing through associative arrays

Posted: 03 Jun 2018, 08:30
by nnnik
It might be important to know that in AHK there is no difference between {} and [].
Both handle integer, string and object keys in the same way.

Re: Looping, scrolling or browsing through associative arrays

Posted: 03 Jun 2018, 10:06
by JemyM
SirRFI wrote:Change IDIndex[69883].Executeable to value.Executeable.
Value contains the contents, so if the contents is another array/object/whatever, you have to access it like you normally would. Think of value as IDIndex[69883] for the moment being.
Of course you can rename key and value variables to suit your need.
That is beautiful! It works perfectly well. Since I commonly refer to the Key as "MobyID" and the value as "information" I ended up with;

Code: Select all

for MobyID, info in IDIndex
MsgBox % "MobyID #" MobyID " has executeable " info.Executeable

Re: Looping, scrolling or browsing through associative arrays

Posted: 03 Jun 2018, 10:36
by JemyM
And after some more effort...

Image