Page 1 of 1

Associative Array inside a Regular Array

Posted: 23 Aug 2015, 15:20
by gilliduck
I'd like the below code to end up making an array that each index contains an associative array containing the values of Name, Price, Option, Quantity & Stock. Basically Array := [{Name:Something,Price:$129.99,Option:Brown,Stock:In Stock},{Name:SomethingElse,Price:$119.99,Option:,Stock:Limited Quantity}. I'm just not sure how to create it. Someone mind tossing me a small bone?

FYI if you run the script, prepare for the UrlDownloadToFile to take 30 seconds or more.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus


Re: Associative Array inside a Regular Array

Posted: 23 Aug 2015, 15:55
by TLM
gilliduck I noticed you left irc before I got to show you this..
you don't have to save to file and use a bunch of substr() things, you can use dom to get the info you're after
http://p.ahkscript.org/?p=0d45fd

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus


Re: Associative Array inside a Regular Array

Posted: 23 Aug 2015, 16:02
by gilliduck
That doesn't seem to do anything. It runs, then exits without ever generating a MsgBox.

Re: Associative Array inside a Regular Array

Posted: 23 Aug 2015, 16:06
by TLM
hrm that's odd I get msgbox's for each item
Image

Re: Associative Array inside a Regular Array

Posted: 23 Aug 2015, 16:29
by TLM
that aside for a sec,
here's an example of inserting associative arrays into simple arrays

Code: [Select all] [Download] GeSHi © Codebox Plus

arr := []
arr[ 1 ] := {a:"dog",b:"cat",c:"bird"}
arr[ 2 ] := {a:"fish",b:"snake",c:"frog"}

for i, obj in arr
for key, animal in obj
msgbox % "Array: " i "`nKey: " key "`nAnimal: " animal

Re: Associative Array inside a Regular Array

Posted: 23 Aug 2015, 16:52
by gilliduck
Yeah, I've got no explanation. I can see it run, but then it eventually exits without ever generating anything. I'll play with it more. Thanks for the Array info!

Re: Associative Array inside a Regular Array

Posted: 24 Aug 2015, 02:02
by lexikos
Basically Array := [{Name:Something,Price:$129.99,Option:Brown,Stock:In Stock},{Name:SomethingElse,Price:$119.99,Option:,Stock:Limited Quantity}
Add quote marks around the strings, append the missing ']', and you'll have it...

...but I guess your actual usage will involve adding items one at a time, like TLM demonstrated.

Re: Associative Array inside a Regular Array

Posted: 24 Aug 2015, 05:42
by gilliduck
Once I had the basic syntax down it made perfect sense. Inside a loop dict[A_Index] := {Item:Name, Price:Price, Option:Option, Stock:Stock} with all values being variables, so no need for quotes to define strings. It's so simple yet I just couldn't see it in the moment.