Page 1 of 1

Understanding Associative arrays

Posted: 12 Dec 2019, 12:45
by eblanc
I'm building a simple mailing list that I want to build using arrays. I've pointed to associative arrays. I've been reading about arrays for a week now. Still don't understand how they work exactly or how you manipulate them. I've looked for tutorials online but they seem pretty vague or convoluted. Please help. Mostly need the theory I think.

So if I want to make a simple list. say.

Name Email
brady [email protected]
jonny [email protected]
Lord Ragnarok [email protected]

I'm guessing I would put it as this:

Code: Select all

Contacts := {brady {"email": "[email protected]"}, (jonny {"email": "[email protected]"}, (Lord Ragnarok {"email": "[email protected]"}}
Correct? just want to make sure syntax is correct also.

so here is my bogus code and as far as I understand.

Code: Select all

array := jonny
Contacts := {brady {"email": "[email protected]"}, (jonny {"email": "[email protected]"}, (Lord Ragnarok {"email": "[email protected]"}}
msgbox, contacts[%array%]??????????
return
My question is, how do I recall information?? I have a feeling that Msgbox is wrong, I also want to make sure I'm using the variables correctly and syntax is correct. I'm not really sure why but I've been really having trouble understanding the logic behind arrays. it seems like a 3rd-dimensional information table that I am having trouble picturing in my brain.

Thanks for the help.

Re: Understanding Associative arrays

Posted: 12 Dec 2019, 12:56
by Tom Harding

Code: Select all

Contacts := {"brady":{"email":"[email protected]"},"sandy":{"email":"[email protected]"}}
MsgBox % Contacts["sandy"].email
There are a few ways to go about this, but this is the quick answer.

Re: Understanding Associative arrays

Posted: 12 Dec 2019, 13:06
by Hellbent
Hope this helps.

Code: Select all


#SingleInstance, Force
#NoEnv
SetBatchLines, -1

Contacts := {Brady: {Email: "[email protected]" , Phone: "123-456-7890"} , Tom: {Email: "" , Phone: ""} , "Mary Ann": {Email: "" , Phone: ""}}

Msgbox,% Contacts.Brady.Email
;or
Msgbox,% Contacts["Brady"].Email
;or
Msgbox,% Contacts["Brady"]["Email"]
;or
Msgbox,% Contacts["Brady","Email"]


;It can sometimes be helpful to break things up a bit.

NewContacts := {}

NewContacts.Sam := {Email: "[email protected]", Phone: "098-765-4321" , "Some key that has spaces in the name": 1234 }
;or
NewContacts.Sam := { Email: "[email protected]"
				   , Phone: "098-765-4321" 
				   , "Some key that has spaces in the name": 1234 }


return
GuiClose:
GuiContextMenu:
*ESC::
	ExitApp

Re: Understanding Associative arrays

Posted: 13 Dec 2019, 08:27
by hd0202

Code: Select all

; msgbox, contacts[%array%]??????????
msgbox, % contacts[array]
Hubert