Associative Array inside a Regular Array

Get help with using AutoHotkey and its commands and hotkeys
gilliduck
Posts: 265
Joined: 06 Oct 2014, 15:01

Associative Array inside a Regular Array

23 Aug 2015, 15:20

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

#SingleInstance, Force

URL := "https://www.overstock.com/registries/wishlists/3119049/Ebay/?view&page=100"
FileName := "overstock.txt"
; Destination := "C:\Users\dgilliland\Desktop\Test\overstock.txt"
Dest_Folder := "C:\Users\Public\Desktop\"
FileArray := []


UrlDownloadToFile, %URL%, % Dest_Folder . FileName
If ErrorLevel <> 0
	MsgBox There was a problem
Loop, Files, %Dest_Folder%, F
	If (A_LoopFileName = FileName)
		Break
Loop, Read, % Dest_Folder . FileName
	FileArray.Push(A_LoopReadLine)
Loop % FileArray.MaxIndex()
	{
	If (InStr(FileArray[A_Index], "wl-item-img"))
		{
		Name := SubStr(SubStr(FileArray[A_Index+1], InStr(FileArray[A_Index + 1], "alt=")+5),1,-6)
		Price := SubStr(SubStr(FileArray[A_Index + 6], InStr(FileArray[A_Index + 6], "$")),1,18)
		Option := (InStr(FileArray[A_Index + 8], "<strong>Option:")) ? "Option: " . SubStr(FileArray[A_Index + 8],41) : ""
		Stock_Increment := (Option = "") ? 0 : 1
		Stock := (Stock_Increment = 0) ? InStr((FileArray[A_Index + 14]), "In Stock") : InStr((FileArray[A_Index + 17]), "In Stock")
		Stock := (Stock <> 0) ? "In Stock" : "Limited Quantity"
		MsgBox % Name "`n" Price "`n" Option "`n" Quantity "`n" Stock
		}
	}
User avatar
TLM
Posts: 1338
Joined: 01 Oct 2013, 07:52

Re: Associative Array inside a Regular Array

23 Aug 2015, 15:55

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

url = https://www.overstock.com/registries/wishlists/3119049/Ebay/?view&page=100

document := URLToObject( url )

itmObj := document.getElementById( "wlItemsWrapper" ).children

Loop % itmObj.length
{
    itm := itmObj[ a_index-1 ]
    itemname	:= itm.getElementsByTagName( "img" )[ 0 ].alt
    imglink		:= itm.getElementsByTagName( "img" )[ 0 ].src
    itemprice	:= itm.getElementsByTagName( "div" )[ 5 ].innerText
    dateadded	:= itm.getElementsByTagName( "div" )[ 0 ].parentNode.getAttribute( "data-added-on" )

    msgbox % "Name: " itemname "`nPrice: " itemprice "`nDateAdded: " dateadded  "`nIMGLink: " imglink
}

return

URLToObject( url )
{
    htReqObj := ComObjCreate( "WinHttp.WinHttpRequest.5.1" ), htFileObj := ComObjCreate( "HTMLfile" )
    htReqObj.Open( "GET", url ), htReqObj.Send(), htFileObj.Write( htReqObj.ResponseText )

    return htFileObj
}
gilliduck
Posts: 265
Joined: 06 Oct 2014, 15:01

Re: Associative Array inside a Regular Array

23 Aug 2015, 16:02

That doesn't seem to do anything. It runs, then exits without ever generating a MsgBox.
User avatar
TLM
Posts: 1338
Joined: 01 Oct 2013, 07:52

Re: Associative Array inside a Regular Array

23 Aug 2015, 16:06

hrm that's odd I get msgbox's for each item
Image
User avatar
TLM
Posts: 1338
Joined: 01 Oct 2013, 07:52

Re: Associative Array inside a Regular Array

23 Aug 2015, 16:29

that aside for a sec,
here's an example of inserting associative arrays into simple arrays

Code: Select all

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
gilliduck
Posts: 265
Joined: 06 Oct 2014, 15:01

Re: Associative Array inside a Regular Array

23 Aug 2015, 16:52

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!
lexikos
Posts: 6201
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Associative Array inside a Regular Array

24 Aug 2015, 02:02

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.
gilliduck
Posts: 265
Joined: 06 Oct 2014, 15:01

Re: Associative Array inside a Regular Array

24 Aug 2015, 05:42

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.

Return to “Ask For Help”

Who is online

Users browsing this forum: Bill, Flipeador, Google [Bot], Mipha, swagfag, Tigerlily and 49 guests