Associate Array of Objects..

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
userXeo1
Posts: 39
Joined: 26 Feb 2020, 09:12

Associate Array of Objects..

22 Oct 2021, 11:07

Hi, thank you for reading. I am stuck for days on this... need bit of help..

I have trouble adding value to an array of objects (I hope I am naming this properly).

I was able to add property "single" in the following matter:

Code: Select all

	People := Array() ; or {}
	People.Rick := {Year: "1986", Relation: "Friend"}
	People.Rick.Status := "Single"
	People.Bob := {Year: "1988", Relation: "Cousin"}
	People.Jack := {Year: "1990", Relation: "Brother"}
	MsgBox % People.Rick.Year . "`r" . People.Rick.Status 
However, I have a file, INI that is constructed in similar matter

Code: Select all

User.property1=value
User.property2=value3xx
Another.property1=valuex
I have a loop that goes trought each line and I am unable to construct the array of objects.. Commented lines are the things I have tried.

Code: Select all

	
	oArray := Array()
	Loop Parse, vText, % "`n", % "`r"
	{
		oTemp := StrSplit(A_LoopField, "=")
		val:=oTemp.2
		vTemp :=StrSplit(oTemp.1, ".")
		key:=vTemp.1
		prop:=vTemp.2

		;oArray[key[A_Index]] := key[A_Index]
		/*
			oArray[key] := key
			oArray[key].key := { (prop) : val }
		*/
		;oArray[key].Insert({(prop) : val })
		;oArray.key.Push(Object(prop, val ))
		oArray[%key%][%prop%] := val
		;oArray.Insert( key, {(prop) :val})
		;oArray .= { (key) : (key) }
		
	}
Target: to obtain value via object oArray.User1.property

I have read many posts and still not sure why I am not successful. Few Listed below.
https://www.autohotkey.com/docs/Objects.htm#Usage_Associative_Arrays
https://www.autohotkey.com/board/topic/77221-associative-array-of-objects-help/
https://www.autohotkey.com/board/topic/80057-insert-for-associative-arrays/
viewtopic.php?t=61349
https://www.autohotkey.com/board/topic/68585-ahk-l-associative-array-insertremove/

Your help is appreciated. Thank you.
User avatar
Spawnova
Posts: 554
Joined: 08 Jul 2015, 00:12
Contact:

Re: Associate Array of Objects..

22 Oct 2021, 11:32

You just about had it =P

Here's how I would do it

Code: Select all

ini =
(
User.property1=value
User.property2=value3xx
Another.property1=value
One.Two.Three.Four=Five
)


array := IniToArray(ini)

msgbox % array.user.property1 "`n" array.user.property2 "`n" array.Another.property1 "`n" array.one.two.three.four
exitapp





IniToArray(iniText) {
	results := []
	
	Loop Parse, iniText,`n,`r
	{
		temp := StrSplit(A_LoopField, "=") ;split key and value
		if (temp.length() = 2) {  ; if not formatted correctly ignore this entry
			value := temp[2]
			temp := StrSplit(temp[1], ".")
			
			lastObj := results ;set the last object to results array
			
			loop % temp.length() - 1 { ;loop through array keys, creating empty array where needed
				if (!lastObj.haskey(temp[a_index]))
					lastObj[temp[a_index]] := []
				lastObj := lastObj[temp[a_index]] ;set the last object to now be the last array we created
			}
			lastObj[temp[temp.length()]] := value ;on the last entry instead of an empty array, assign the value
			
		}
	}
	
	return results
}
userXeo1
Posts: 39
Joined: 26 Feb 2020, 09:12

Re: Associate Array of Objects..

22 Oct 2021, 22:01

Thank you Spaenova so much for your reply..😆 at almost had it. Not even close.. . I'll take me few day's to digest this when I get free min. crazy day at work today with covid shots and more tomorrow!.. I really appreciate your help. Thank you 😊

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Freddie, gongnl, mikeyww, mmflume, OrangeCat, ShatterCoder and 88 guests