Page 1 of 1
How to find value in associative array
Posted: 29 Jun 2018, 17:11
by drizzt
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
Re: How to find value in associative array
Posted: 29 Jun 2018, 22:32
by rommmcek
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
Re: How to find value in associative array
Posted: 30 Jun 2018, 02:32
by drizzt
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.
Re: How to find value in associative array
Posted: 30 Jun 2018, 06:10
by rommmcek
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.
Re: How to find value in associative array
Posted: 30 Jun 2018, 09:46
by gregster
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
Re: How to find value in associative array
Posted: 30 Jun 2018, 09:51
by swagfag
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
Re: How to find value in associative array
Posted: 30 Jun 2018, 09:53
by gregster
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
Re: How to find value in associative array
Posted: 30 Jun 2018, 09:57
by gregster
I think, there is a rogue/misplaced , Result := [] in your code, swagfag.
Re: How to find value in associative array
Posted: 30 Jun 2018, 10:07
by swagfag
gregster wrote:I think, there is a rogue/misplaced , Result := [] in your code, swagfag.
nothing wrong with it as far as i can tell
Re: How to find value in associative array
Posted: 30 Jun 2018, 10:12
by gregster
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...
Re: How to find value in associative array
Posted: 30 Jun 2018, 21:31
by drizzt
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
}
}
Re: How to find value in associative array Topic is solved
Posted: 30 Jun 2018, 21:56
by Xtra
Is always true.
Try using:
HTH
Re: How to find value in associative array
Posted: 01 Jul 2018, 01:19
by drizzt
Thanks!