How to find value in associative array Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
drizzt
Posts: 31
Joined: 20 Apr 2018, 20:44

How to find value in associative array

29 Jun 2018, 17:11

I have a script where I get info from a tree control and store in an array as follows

Code: Select all

Tree_Array[oKey]:= {mylocationis: vAccLocation, mynameis: vName, mykey: oKey}
I now want to be able to search a specific name in this array and find out what its location is but have looked through the documentation and there is no search for associative arrays
User avatar
rommmcek
Posts: 1470
Joined: 15 Aug 2014, 15:18

Re: How to find value in associative array

29 Jun 2018, 22:32

I'm certainly no expert in Arrays, but your assignement seems to be wrong. Try:

Code: Select all

#NoEnv
;~ Tree_Array[oKey]:= {mylocationis: vAccLocation, mynameis: vName, mykey: oKey}
Tree_Array:= {"mylocationis": ["vAccLocation", "sss", "www"], "mynameis": ["vName", "qqq"], "mykey": ["oKey", "rrr"]}
for subArr, Arr in Tree_Array
	for j in Arr
		MsgBox % SubArr " > " Tree_Array[SubArr][j]
esc::ExitApp
drizzt
Posts: 31
Joined: 20 Apr 2018, 20:44

Re: How to find value in associative array

30 Jun 2018, 02:32

rommmcek wrote:I'm certainly no expert in Arrays, but your assignement seems to be wrong. Try:

Code: Select all

#NoEnv
;~ Tree_Array[oKey]:= {mylocationis: vAccLocation, mynameis: vName, mykey: oKey}
Tree_Array:= {"mylocationis": ["vAccLocation", "sss", "www"], "mynameis": ["vName", "qqq"], "mykey": ["oKey", "rrr"]}
for subArr, Arr in Tree_Array
	for j in Arr
		MsgBox % SubArr " > " Tree_Array[SubArr][j]
esc::ExitApp

I am not assigning them like that..that line is in a loop where every okey has a different location, name etc.
User avatar
rommmcek
Posts: 1470
Joined: 15 Aug 2014, 15:18

Re: How to find value in associative array

30 Jun 2018, 06:10

I might be wrong but:

Code: Select all

if IsObject(Tree_Array)
	MsgBox Assignement is O.K.
else MsgBox Assignement seems to be false
doesn't give good result for your assignement.
gregster
Posts: 8916
Joined: 30 Sep 2013, 06:48

Re: How to find value in associative array

30 Jun 2018, 09:46

Afaik, you have to search for/loop through values of an assoc. array yourself.

Code: Select all

Tree_Array := {}	; create an assoc. array

Tree_Array[111]:= {mylocationis: "Paris", mynameis: "Jim", mykey: oKey}
Tree_Array[222]:= {mylocationis: "Berlin", mynameis: "Tim", mykey: oKey}
Tree_Array[333]:= {mylocationis: "Madrid", mynameis: "Tom", mykey: oKey}

for key, Arr in Tree_Array
{
	If (Arr.mynameis = "Tom")
		msgbox % "Key: " key "`nTom's location: " Arr.mylocationis
	else
		msgbox % "Key: " key "  " Arr.mynameis "`nNo, it's not Tom!"
}
ExitApp
esc::ExitApp
Last edited by gregster on 30 Jun 2018, 09:58, edited 1 time in total.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: How to find value in associative array

30 Jun 2018, 09:51

u can either traverse the entire array looking for the value that youre interested in(what happens when u find multiple entries with the same name but different location? do u break on the first match, do u aggregate them and keep going?):

Code: Select all

Tree_Array[oKey]:= {mylocationis: vAccLocation, mynameis: vName, mykey: oKey}

for each, Entry in Tree_Array, Result := []
	if (Entry.mynameis == "your_name_lookup")
		Result.Push(Entry)

for each, Entry in Result
	MsgBox % Entry.mylocationis
or you can keep a second array alongside mapping the name to a key and look for it there
also the fact that oKey appears twice is smelly, Id rethink the way youre going about storing these things
Last edited by swagfag on 30 Jun 2018, 09:53, edited 1 time in total.
gregster
Posts: 8916
Joined: 30 Sep 2013, 06:48

Re: How to find value in associative array

30 Jun 2018, 09:53

rommmcek wrote:I might be wrong but:

Code: Select all

if IsObject(Tree_Array)
	MsgBox Assignement is O.K.
else MsgBox Assignement seems to be false
doesn't give good result for your assignement.
Afaik, if you are using the [ ] syntax for assigning, you need the associative array to be created before via { } or Object(): https://autohotkey.com/docs/Objects.htm ... ive_Arrays

Code: Select all

Tree_Array := {}	; create an assoc. array
; obviously, usually you would fill the variables somewhere here
Tree_Array[oKey]:= {mylocationis: vAccLocation, mynameis: vName, mykey: oKey}
if IsObject(Tree_Array)
	MsgBox Assignement is O.K.
else MsgBox Assignement seems to be false
gregster
Posts: 8916
Joined: 30 Sep 2013, 06:48

Re: How to find value in associative array

30 Jun 2018, 09:57

I think, there is a rogue/misplaced , Result := [] in your code, swagfag.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: How to find value in associative array

30 Jun 2018, 10:07

gregster wrote:I think, there is a rogue/misplaced , Result := [] in your code, swagfag.
nothing wrong with it as far as i can tell
gregster
Posts: 8916
Joined: 30 Sep 2013, 06:48

Re: How to find value in associative array

30 Jun 2018, 10:12

swagfag wrote:
gregster wrote:I think, there is a rogue/misplaced , Result := [] in your code, swagfag.
nothing wrong with it as far as i can tell
Oh yeah, I really didn't know that this was allowed on the same line with a for-loop. Sorry...
drizzt
Posts: 31
Joined: 20 Apr 2018, 20:44

Re: How to find value in associative array

30 Jun 2018, 21:31

OK I decided to reevaluate the way i was doing things. now im using a simple multidimensional array instead of associative arrays...

assignment

Code: Select all

Tree_Array2:={}

	Tree_Array2[oKey,0]:=vAccLocation  ; these are in a loop somewhere
	Tree_Array2[oKey,1]:=vName  ; these are in a loop somewhere
I know its assigned properly because I can see it with this

Code: Select all

	For Each, Row in Tree_array2
		For, Each, item in Row
		MsgBox %item%
The only problem is that it seems to find a match every time. shouldnt it only display "John" once?

Code: Select all

	test:="sss"
	i:=0
	j:=0
	For each, Row in Tree_array2
	{	i:=i+1
		j:=0
		
		For each , item in Row
		{
		
				test:=Tree_Array2[i,j]
	
			
			if test:="John"
				MsgBox found
			MsgBox %i% . " " . %j%
				j :=j+1
		}
		
		
	}
	
	
User avatar
Xtra
Posts: 2744
Joined: 02 Oct 2015, 12:15

Re: How to find value in associative array  Topic is solved

30 Jun 2018, 21:56

Code: Select all

			if test:="John"
				MsgBox found
Is always true.

Try using:

Code: Select all

			if (test="John")
				MsgBox found
HTH

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: CaseyMoon, Google [Bot], mmflume, Rohwedder, ShatterCoder and 181 guests