How to create 2+ Nested Associtive Arrays? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Renets
Posts: 36
Joined: 12 Jul 2018, 13:11

How to create 2+ Nested Associtive Arrays?

17 Dec 2019, 16:29

After trying hour and hours I can't get this working. Already did a search about related topics and I couldn't find anything useful for this challenge.
I had to turn to your help guys, thanks in advance

So, I need to have this structure:

Code: Select all

MyArray
     option_1
          index_1
               value
          index_2
               value 
          index_3
               value 
          index_4
               value 
          index_5
               value 
     option_2
          index_1
               value
          index_2
               value 
          index_3
               value 
Then my goal is to be able to use this sentence to get the Count() of a specific "option"

MyArray[option_2].Count() ; It should give 3, not 5, which is the "option_1"

I am facing a challenge because by using the Insert() method doesn't nest beyond two indexes (two nested arrays). I can do this:

; here "option_handler" is "option_1"
MyArray.Insert(option_handler, {"index": index_value, "value": value_handler})

But that will give this kind structure

MyArray
option_1
index_1
value
option_1
index_2
value
option_1
index_3
value

The "option_1" values are separated, what I need is for those to be nested within "option_1", like in the structure I set in the beginning
This would be my ideal alternative, which is not working

MyArray.Insert(option_handler, { index_value, {"value": value_handler}})

Notes
"option_handler" is a variable that will change depending on the option ("option_1" or "option_2")
"index_value" is a variable that will change depending on the index_value("1", "2", "3"...)
"value_handler" is a variable that will change depending on the value_handler("aa", "bb", "cc"...)

Any recommendation is appreciated!
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: How to create 2+ Nested Associtive Arrays?

17 Dec 2019, 16:52

its not clear what kind of structure u need. do u want it like so:

Code: Select all

{
	"option_1":
	{
		"index_1": value,
		"index_2": value,
		"index_3": value,
		"index_4": value,
		"index_5": value
	},
	"option_2":
	{
		"index_1": value,
		"index_2": value,
		"index_3": value
	}
}
or like so:

Code: Select all

{
	"option_1":
	[
		{ "index": index_value, "value": value_handler },
		{ "index": index_value, "value": value_handler },
		{ "index": index_value, "value": value_handler },
		{ "index": index_value, "value": value_handler },
		{ "index": index_value, "value": value_handler }
	],
	"option_2":
	[
		{ "index": index_value, "value": value_handler },
		{ "index": index_value, "value": value_handler },
		{ "index": index_value, "value": value_handler }
	]
}
Renets
Posts: 36
Joined: 12 Jul 2018, 13:11

Re: How to create 2+ Nested Associtive Arrays?

17 Dec 2019, 16:59

swagfag wrote:
17 Dec 2019, 16:52

Code: Select all

{
	"option_1":
	{
		"index_1": value,
		"index_2": value,
		"index_3": value,
		"index_4": value,
		"index_5": value
	},
	"option_2":
	{
		"index_1": value,
		"index_2": value,
		"index_3": value
	}
}
preferably this first one but it would also be useful to know how to do that second one :)
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: How to create 2+ Nested Associtive Arrays?  Topic is solved

17 Dec 2019, 17:40

in both cases MyArray must be declared an object:

Code: Select all

MyArray := {}

then for the first case, every "option" key u define needs to be associated with an object:

Code: Select all

MyArray[option_handler] := {}
if all the contents(the index/val entries) are known u can just assign them as an object in one go, otherwise an empty object has to be assigned and the entries have to be inserted after that sequentially

Code: Select all

MyArray[option_handler, index_value] := value_handler

in the second case, u will still have to define "option" keys but instead u will associated them with arrays:

Code: Select all

MyArray[option_handler] := []
then u can .Push() to these arrays objects { "index": index_value, "value": value_handler } and the indexing will be handled automatically for u:

Code: Select all

MyArray[option_handler].Push({"index": index_value, "value": value_handler})
Renets
Posts: 36
Joined: 12 Jul 2018, 13:11

Re: How to create 2+ Nested Associtive Arrays?

18 Dec 2019, 18:58

swagfag wrote:
17 Dec 2019, 17:40
in the second case, u will still have to define "option" keys but instead u will associated them with arrays:

Code: Select all

MyArray[option_handler] := []
then u can .Push() to these arrays objects { "index": index_value, "value": value_handler } and the indexing will be handled automatically for u:

Code: Select all

MyArray[option_handler].Push({"index": index_value, "value": value_handler})
Excellent!
Sorry about the late reply

I ended up using the second one, it fit the best to my needs

While I was trying to get this working before I realize I could also add the entire array into another array with a Push() like so:

Code: Select all

index_value := 1
value := "aa"
options_handler:= "page_1"

main_array:=[]
my_nested_array_page_1:=[]
my_nested_array_page_2:=[]

my_nested_array_page_1.Insert(index_value, value)
main_array.Insert(options_handler, my_nested_array_page_1)
index_value++
my_nested_array_page_1.Insert(index_value, value)
main_array.Insert(options_handler, my_nested_array_page_1)
index_value++
my_nested_array_page_1.Insert(index_value, value)
main_array.Insert(options_handler, my_nested_array_page_1)
index_value++
my_nested_array_page_1.Insert(index_value, value)
main_array.Insert(options_handler, my_nested_array_page_1)
index_value++

options_handler:= "page_2"
value := "bb"
index_value := 1

my_nested_array_page_2.Insert(index_value, value)
main_array.Insert(options_handler, my_nested_array_page_2) 
index_value++
my_nested_array_page_2.Insert(index_value, value)
main_array.Insert(options_handler, my_nested_array_page_2) 


for a, b in main_array {
    MsgBox % a " -A- " b
    for c, d in b {
        MsgBox % c " -C- " d
    }
}

MsgBox %  "CN " main_array["page_2"].Count() ; Display "2"
That's another alternative but the solution you mentioned is more flexible and most optimized, thank you!

PD: I now recall your username, I've seen you in many posts of AHK, glad to interact with you!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], Rohwedder, Tech Stuff and 335 guests