Array/Object Add If Function Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
HeXaDeCiMaToR
Posts: 155
Joined: 08 Feb 2021, 12:42

Array/Object Add If Function

Post by HeXaDeCiMaToR » 28 May 2022, 10:41

I'm trying to create a function that will "push" key/value into an Object or Array (I'm only at the "Add if to Object" section), and I'm getting empty returns.

No issues when using the script below outside of a function, but I want to tighten up the script.

Code: Select all

if !(i.vin=""||i.vin="null"||i.vin="false")
	oSub["vin"] := i.vin
Current Function:

Code: Select all

Array_AddIf(Arr, Key, Value) {
	if !(Value=""||Value="null"||Value="false")
		Arr[Key] := Value
	return Arr
}
Current Script Commands (there's a lot of debug script in there still):

Code: Select all

MergeTrailerTrader:
oTrailerTraderType1 := {}
j := 0
Loop, E:\Folder\Temp File Folder For Logs\TrailerTraderType1*.txt
{
	FileRead, v, E:\folder\Temp File Folder For Logs\%A_LoopFileName%
	v := json.load(v)
	if !IsObject(v)
		MsgBox, 262144, Error!, Object Creation Unsucessful
	j += v.data.inventories.count()
	ToolTip, % j
	;~ continue
	for e, i in v.data.inventories
	{
		oTrailerTraderType1[i.type_label] := {} 
		oSubTT := oTrailerTraderType1[i.type_label]
		oSubTT[i.id] := {}
		oSub := oSubTT[i.id]
		
		oSub := Array_AddIf(oSub, "title", i.title)
		oSub := Array_AddIf(oSub, "vin", i.vin)
		oSub := Array_AddIf(oSub, "year", i.year)
		oSub := Array_AddIf(oSub, "make", i.manufacturer)
		oSub := Array_AddIf(oSub, "model", i.model)
		oSub := Array_AddIf(oSub, "condition", i.condition)
		oSub := Array_AddIf(oSub, "pull_type", i.pull_type)
		oSub := Array_AddIf(oSub, "num_stalls", i.num_stalls)
		oSub := Array_AddIf(oSub, "load_type", i.load_type)
		oSub := Array_AddIf(oSub, "num_slideouts", i.num_slideouts)
		oSub := Array_AddIf(oSub, "width_inches", i.width_inches)
		oSub := Array_AddIf(oSub, "length_inches", i.length_inches)
		oSub := Array_AddIf(oSub, "length", i.length)
		oSub := Array_AddIf(oSub, "gvwr", i.gvwr)
		oSub := Array_AddIf(oSub, "axle_capacity", i.axle_capacity)
		oSub := Array_AddIf(oSub, "stock", i.stock)
		if !(oSub.stock="")
		{
			RegExMatch(i.stock, "\d++", stock)
			linkTag := "/all-inventory?stock=" stock
		}
		oSub := Array_AddIf(oSub, "website_price", i.website_price)
		oSub := Array_AddIf(oSub, "description", i.description)
		oSub := Array_AddIf(oSub, "dealer", i.location.name)
		oSub := Array_AddIf(oSub, "phone", i.location.phone)
		oSub := Array_AddIf(oSub, "city", i.location.city)
		oSub := Array_AddIf(oSub, "state", i.location.region)
		oSub := Array_AddIf(oSub, "link", i.location.website . linktag)
		oSub := Array_AddIf(oSub, "stock", i.stock)
		oSub := Array_AddIf(oSub, "stock", i.stock)
		
		oImages := oSub["Images"]
		for k, v in i.images
			oImages.push(v)
	}
	MsgBox, 262144, oSub.count, % oSub.count()
	for l, u in oSub
		MsgBox, 262144, l/u, % l "`n`n" u
	ToolTip, % oTrailerTraderType1.count()
}

;~ v := json.dump(oTrailerTraderType1)
;~ FileAppend, %v%, oTrailerTraderType1_20220527.txt
MsgBox, 262144, Merge Status:, DONE (%j%)
return
**Text File (json format) original information is being pulled from is the file in attachments.

I'm sorry @Chunjee!! I just haven't got around to learning/adding/using your array/object class :cry:

(if I can't get this function ironed out I'm just going to drop down into the next line of v.data.inventories with another for each and use each and item for object.push(element))

**Edit: I noticed that I hadn't declared oSubTT[i.id] as an object oSubTT[i.id] := {} added
**Edit: I remembered why I didn't add that declaration as it would erase all the information on each itteration... oSubTT[i.id] := {} removed
**Edit: 10,000 Foot view... Added oSubTT[i.id] := {} back in AND Added oTrailerTraderType1[i.type_label] := {} in (to declare both as an object), and my current script seems to be working (no function). i'm going to switch it back to the function and test.
Last edited by HeXaDeCiMaToR on 28 May 2022, 11:53, edited 3 times in total.

HeXaDeCiMaToR
Posts: 155
Joined: 08 Feb 2021, 12:42

Re: Array/Object Add If Function

Post by HeXaDeCiMaToR » 28 May 2022, 10:50

I don't see the attachment in the OP. Looks like there's a 2 MB limit and it's 5.6 MB...


HeXaDeCiMaToR
Posts: 155
Joined: 08 Feb 2021, 12:42

Re: Array/Object Add If Function  Topic is solved

Post by HeXaDeCiMaToR » 28 May 2022, 12:19

Fixed:

Function:

Code: Select all

Array_AddIf(Arr, Key, Value) {
	if !(Value=""||Value="null"||Value="false"||Value="0"||Value="0.00")
		Arr[Key] := Value
	return Arr
}
Script:

Code: Select all

MergeTrailerTrader:
oTrailerTraderType1 := {}
j := 0
Loop, E:\Folder\Temp File Folder For Logs\TrailerTraderType1*.txt
{
	FileRead, v, E:\Folder\Temp File Folder For Logs\%A_LoopFileName%
	v := json.load(v)
	j += v.data.inventories.count()
	ToolTip, % j
	for e, i in v.data.inventories
	{
		if !IsObject(oTrailerTraderType1[i.type_label])
			oTrailerTraderType1[i.type_label] := {}
		oSubTT := oTrailerTraderType1[i.type_label]
		oSubTT[i.id] := {}
		oSub := oSubTT[i.id]
		for each, item in i
		{
			oSub := Array_AddIf(oSub, each, item)
			if (each="images")
			{
				oSub["images"] := {}
				oImages := oSub["images"]
				for k, v in i.images
					oImages[A_Index] := v
			}
			if (each="dealer")
			{
				oSub["dealer"] := {}
				oDealer := oSub["dealer"]
				for k, v in i.dealer
				{
					if !(v=""||v="null"||v="false")
						oDealer := Array_AddIf(oDealer, k, v)
				}
			}
			if (each="location")
			{
				oSub["location"] := {}
				oLocation := oSub["location"]
				for k, v in i.location
				{
					if !(v=""||v="null"||v="false")
						oLocation := Array_AddIf(oLocation, k, v)
				}
			}
			if (each="feature_list")
			{
				oSub["feature_list"] := {}
				oLocation := oSub["feature_list"]
				for k, v in i.feature_list
				{
					if !(v=""||v="null"||v="false")
						oFeature_List := Array_AddIf(oFeature_List, k, v)
				}
			}
			if (each="keywords")
				continue
		}
	}
}
v := json.dump(oTrailerTraderType1)
FormatTime, TimeStr, %A_Now%, yyyyMMdd
FileAppend, %v%, oTrailerTraderType1_%timeStr%.txt
MsgBox, 262144, Merge Status:, DONE (%j%)
return
**Edit: After testing I noticed that the if oTrailerTraderType1[i.type_label] := {} line was erasing the category on every new itteration of v.data.inventories. So, I added the following and edited the script above.

Code: Select all

if !IsObject(oTrailerTraderType1[i.type_label])
	oTrailerTraderType1[i.type_label] := {}

Post Reply

Return to “Ask for Help (v1)”