Retrieving values of dynamic associative array keys Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
somsoc
Posts: 10
Joined: 19 Jul 2017, 18:47

Retrieving values of dynamic associative array keys

19 Jul 2017, 19:06

What's the correct method for returning the value of a associative array key, when the key name is dynamically constructed elsewhere? I had a lot of trouble getting the values stored correctly in the first place! Now I know they are correctly stored (I can manually type the keys and return values okay), but my attempts to construct keynames dynamically are not working.

I assume there is some correct syntax or method I am missing. All I get is "ambigious or invalid use of ." I guess it's thinking I am concatenating something.

My array names and key names look like 'SomeText_MoreText'

I'd like this to retrieve the specified key's value but it doesn't:

Code: Select all

keyFragment1 = foo
keyFragment2 = bar
value := apple_pie.%keyFragment1%_%keyFragment2%
However having the object name be partly built out of variables works fine:

Code: Select all

arrayFragment = pie
value := apple_%arrayFragment%.foo_bar
I've tried enclosing the variable parts in all kinds of brackets. I've tried building both the object name and the variable separately and doing

Code: Select all

value := [object].[keyname]
but that doesn't work, even though it's documented in some places.

All the info I can find assumes that people are manually typing keynames and array names but I can't do this.

Any help appreciated!
User avatar
runie
Posts: 304
Joined: 03 May 2014, 14:50
Contact:

Re: Retrieving values of dynamic associative array keys

19 Jul 2017, 19:33

Could you show how you made the array in the first place?

Retrieving a value from an array is as simple as:

Code: Select all

object[key]
If you have a 2d array you can do:

Code: Select all

object[key1, key2]
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Retrieving values of dynamic associative array keys  Topic is solved

19 Jul 2017, 20:24

Code: Select all

q::
oArray := {abcde:"ABCDE"}
vTemp := "bcd"
MsgBox, % oArray["a" vTemp "e"]
oArray := ""
return

w::
(oArray := {})["abc","def","ghi"] := "hello"
vTemp1 := "b"
vTemp2 := "e"
vTemp3 := "h"
MsgBox, % oArray["a" vTemp1 "c", "def", "ghi"]
MsgBox, % oArray["abc", "d" vTemp2 "f", "ghi"]
MsgBox, % oArray["abc", "def", "g" vTemp3 "i"]

MsgBox, % oArray["a" vTemp1 "c"].def.ghi
MsgBox, % oArray.abc["d" vTemp2 "f"].ghi
MsgBox, % oArray.abc.def["g" vTemp3 "i"]
oArray := ""
return
I think oldbrother posted roughly the same question earlier:
Please help. use loop to access class variables - AutoHotkey Community
https://autohotkey.com//boards/viewtopi ... =5&t=34715

Cheers.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
somsoc
Posts: 10
Joined: 19 Jul 2017, 18:47

Re: Retrieving values of dynamic associative array keys

20 Jul 2017, 09:54

Thanks for the replies.
RUNIE wrote:Could you show how you made the array in the first place?
Sure. This is the assignment used to create it (within a parsing loop):

Code: Select all

arraySet_%stringTempAbbrev%_%stringFileDate%.Insert((stringTempKeyName),(stringTempValue))
stringTempKeyName and stringTempValue are string variables constructed beforehand within the loop.

This creates an object like 'arraySet_ABC_20070720' and key 'key_Name_Goes_Here'.

When I am retrieving my values from the keys, I am trying to do it within another similar loop that again constructs the correct array object name and keynames before I try to use them.

I know one method is to loop through the entire object (for S_KEY,S_VAL in obj...), stopping when I hit my keyname, and pulling out the value, but since I have hundreds (and potentially thousands) of keys I don't really want to do this if I can avoid it.
jeeswg wrote:

Code: Select all

q::
oArray := {abcde:"ABCDE"}
vTemp := "bcd"
MsgBox, % oArray["a" vTemp "e"]
oArray := ""
return
Ahh. I think this looks promising! The syntax of how AHK wants me to form my keynames with variables wasn't clear to me! For me it's more like

Code: Select all

oArray := {foo_BarData1:"ABCDE"}
vTemp := "foo_Bar"
vTemp2 := "Data1"
value:= oArray[vTemp vTemp2]
This seems to work in a quick test. I'll try and implement this later. Thanks very much :)

Would it be possible for you to explain this line in the second example? I can't quite get my head around this syntax! (There's two assignments on the same line?)
jeeswg wrote:

Code: Select all

(oArray := {})["abc","def","ghi"] := "hello"
jeeswg wrote:I think oldbrother posted roughly the same question earlier:
Please help. use loop to access class variables - AutoHotkey Community
https://autohotkey.com//boards/viewtopi ... =5&t=34715
Yeah I read that thread, it seemed to be close to what I was asking but I'll be honest I couldn't make sense of any of the examples!
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Retrieving values of dynamic associative array keys

20 Jul 2017, 11:18

Code: Select all

;the idea here is to combine two lines into one:
oArray := {}
oArray["abc","def","ghi"] := "hello"

(oArray := {})["abc","def","ghi"] := "hello"
objects: define succinctly, retrieve dynamically - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=33530
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
somsoc
Posts: 10
Joined: 19 Jul 2017, 18:47

Re: Retrieving values of dynamic associative array keys

20 Jul 2017, 12:12

jeeswg wrote:

Code: Select all

;the idea here is to combine two lines into one:
oArray := {}
oArray["abc","def","ghi"] := "hello"

(oArray := {})["abc","def","ghi"] := "hello"
objects: define succinctly, retrieve dynamically - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=33530
Okay, I think I see it now. Thanks. That thread however is a bit over my head. But good reading nonetheless :)
User avatar
runie
Posts: 304
Joined: 03 May 2014, 14:50
Contact:

Re: Retrieving values of dynamic associative array keys

20 Jul 2017, 12:43

You need to use objects and not pseudo-arrays to use the object functions. To be honest the documentation on pseudo-arrays should be removed from the documentation since it only leads new users down the wrong paths..
somsoc
Posts: 10
Joined: 19 Jul 2017, 18:47

Re: Retrieving values of dynamic associative array keys

22 Jul 2017, 19:02

RUNIE wrote:You need to use objects and not pseudo-arrays to use the object functions. To be honest the documentation on pseudo-arrays should be removed from the documentation since it only leads new users down the wrong paths..
I started out using pseudo arrays and moved on from them recently when they became unmanageable. I still think they're easier to understand and debug for newcomers, though, just because I can look in the variable list and see the contents of each array and know it's working properly. All I get with the objects is a memory reference (?) which doesn't tell me what's in it. Much more work to debug them.

(it would be nice if objects and/or variables in general were expandable in the variables list!)

Now that I have the issue in this thread sorted, though, I doubt I'll go back to using pseudo arrays very often, since pulling values from keys is much nicer than a whole heap of variables.
User avatar
runie
Posts: 304
Joined: 03 May 2014, 14:50
Contact:

Re: Retrieving values of dynamic associative array keys

22 Jul 2017, 19:08

somsoc wrote:I still think they're easier to understand and debug for newcomers, though, just because I can look in the variable list and see the contents of each array and know it's working properly. All I get with the objects is a memory reference (?) which doesn't tell me what's in it. Much more work to debug them.
Here:

Code: Select all

m(x*) {
	for a, b in x
		text .= (IsObject(b)?pa(b):b) "`n"
	MsgBox, 0, msgbox, % text
}

pa(array, depth=5, indentLevel:="   ") {
	try {
		for k,v in Array {
			lst.= indentLevel "[" k "]"
			if (IsObject(v) && depth>1)
				lst.="`n" pa(v, depth-1, indentLevel . "    ")
			else
				lst.=" -> " v
			lst.="`n"
		} return rtrim(lst, "`r`n `t")	
	} return
}
Simply do

Code: Select all

m(array)
to view the contents of your object/array.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: RandomBoy and 268 guests