Insert obj to another obj without loop?

Get help with using AutoHotkey and its commands and hotkeys
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Insert obj to another obj without loop?

20 Mar 2015, 15:26

For example,

Code: Select all

obj_all := {k1: "v1", k2: "v2"}
obj_sub := {k3: "v3", k4: "v4"}
Can I insert obj_sub into obj_all, which is result in obj_all := {k1, "v1", k2: "v2", k3: "v3", k4: "v4"}, and without use Loop method? thanks.
User avatar
maestrith
Posts: 825
Joined: 16 Oct 2013, 13:52

Re: Insert obj to another obj without loop?

20 Mar 2015, 15:34

I would be interested in this as well. Currently I use a for loop to add values to an object.
John H Wilson III 05/29/51 - 03/01/2020. You will be missed.AHK Studio OSDGUI Creator
Donations
Discord
All code is done on a 64 bit Windows 10 PC Running AutoHotkey x32
Coco-guest

Re: Insert obj to another obj without loop?

20 Mar 2015, 16:01

I don't think that this is possible with associative arrays. However, not really an actual merge but just to avoid the loop:

Code: Select all

obj_all := {k1: "v1", k2: "v2"}
obj_sub := {k3: "v3", k4: "v4"}
obj_all.base := obj_sub

MsgBox % "k3=" . obj_all.k3 . "`nk4=" . obj_all.k4
The down side is, if you need to enumerate(using For-loop) the final object, the other key-value pairs won't be available since they don't really exist in the object.
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: Insert obj to another obj without loop?

20 Mar 2015, 17:19

Thanks Coco! That's already useful :)
User avatar
maestrith
Posts: 825
Joined: 16 Oct 2013, 13:52

Re: Insert obj to another obj without loop?

21 Mar 2015, 01:17

wow, that's nifty :) I had no idea you could do that.
Edit:

Code: Select all

obj_all := {k1: "v1", k2: "v2"}
obj_sub := {k3: "v3", k4: "v4"}
obj_sub2 := {k5: 1, k6: 2}
obj_all.base := obj_sub
obj_all.base := obj_sub2
for key,value in obj_all
	msgbox % key "`n" value
MsgBox % obj_all.k3 ;returns null
return
anything past the original obj_all does not get retrieved from the for loop and if you do it again it messes up the second set (k3 and k4)
John H Wilson III 05/29/51 - 03/01/2020. You will be missed.AHK Studio OSDGUI Creator
Donations
Discord
All code is done on a 64 bit Windows 10 PC Running AutoHotkey x32
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
GitHub: cocobelgica

Re: Insert obj to another obj without loop?

21 Mar 2015, 09:24

@maestrith, obj_all.base := obj_sub2 overrides obj_all's previous base object(which is obj_sub) so it won't be able to access obj_sub's keys. For-loop will only enumerate obj_all's own key-value pairs because ObjHasKey(obj_all, key_name_in_base_object) == 0. Unless you implement a custom _NewEnum()...
User avatar
maestrith
Posts: 825
Joined: 16 Oct 2013, 13:52

Re: Insert obj to another obj without loop?

21 Mar 2015, 14:09

right, but for me I need those new items in the loop. I could not use this but it is neat :)
John H Wilson III 05/29/51 - 03/01/2020. You will be missed.AHK Studio OSDGUI Creator
Donations
Discord
All code is done on a 64 bit Windows 10 PC Running AutoHotkey x32
Getfree
Posts: 231
Joined: 12 Oct 2014, 18:00

Re: Insert obj to another obj without loop?

21 Mar 2015, 15:08

This is how I do it:

Code: Select all

addObjects(targetObj,objsToadd*){
	for i, obj in objsToAdd
		for key, value in obj
			targetObj[key] := value
	return targetObj
}
Examples:

addObjects(a,b) Adds content of b to a

addObjects(a,b,c) Adds content of b and c to a

addObjects(a,b,c,d) Same as above but more objects

newObj := addObjects({},a,b) Merges the contents of a and b into a new object (neither a nor b are modified)
Last edited by Getfree on 21 Mar 2015, 15:09, edited 1 time in total.
User avatar
maestrith
Posts: 825
Joined: 16 Oct 2013, 13:52

Re: Insert obj to another obj without loop?

21 Mar 2015, 15:09

Right, but the original post wanted it done without the for loops.
John H Wilson III 05/29/51 - 03/01/2020. You will be missed.AHK Studio OSDGUI Creator
Donations
Discord
All code is done on a 64 bit Windows 10 PC Running AutoHotkey x32
Getfree
Posts: 231
Joined: 12 Oct 2014, 18:00

Re: Insert obj to another obj without loop?

21 Mar 2015, 15:11

maestrith wrote:Right, but the original post wanted it done without the for loops.
Right, but it's been establish already that it is not possible without loops. So this is the next best thing.
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
GitHub: cocobelgica

Re: Insert obj to another obj without loop?

21 Mar 2015, 15:32

@maestrith, you can use a custom object:

Code: Select all

obj1 := new Mergeable()
obj1.Merge(obj2 := {a:"Auto", h:"Hot", k:"key"})
obj1.a := "AHK"
MsgBox % obj1.a " = " obj2.a ; real-time update
for k, v in obj1 ; you can access the merged object's key-value pairs
	MsgBox %k% = %v%
ExitApp

class Mergeable
{
	__New(KeyVal*)
	{
		ObjInsert(this, Chr(1421), [])
		if len := Round(KeyVal.MaxIndex())
			Loop % len//2
				this[ KeyVal[(A_Index*2)-1] ] := KeyVal[A_Index*2]
	}

	Merge(obj)
	{
		return ObjInsert(this[Chr(1421)], obj)
	}

	__Get(key, args*)
	{
		if (key != "base" && key != "__Class" && !ObjHasKey(Mergeable, key))
		{
			for i, obj in this[Chr(1421)]
				if ObjHasKey(obj, key)
					return obj[key, args*]
		}
	}

	__Set(key, val, args*)
	{
		for i, obj in this[Chr(1421)]
			if ObjHasKey(obj, key)
				return obj[key] := val
	}

	_NewEnum()
	{
		self := ObjClone(this), ObjRemove(self, Chr(1421))
		return { base: {Next: Mergeable._EnumNext}, enum: ObjNewEnum(self), merged: ObjClone(this[Chr(1421)]) }
	}

	_EnumNext(ByRef key, ByRef val:="")
	{
		while !r := this.enum.Next(key, val)
		{
			merged := this.merged
			if !r := merged.MaxIndex()
				break
			this.enum := ObjNewEnum(ObjRemove(merged, 1))
		}
		return r
	}
}
The code above can be improved of course :)
User avatar
maestrith
Posts: 825
Joined: 16 Oct 2013, 13:52

Re: Insert obj to another obj without loop?

21 Mar 2015, 15:36

Coco wrote:@maestrith, you can use a custom object:
The code above can be improved of course :)
LOL I personally do not mind using the loops :) it is a lot less code and doesn't take all that long.
John H Wilson III 05/29/51 - 03/01/2020. You will be missed.AHK Studio OSDGUI Creator
Donations
Discord
All code is done on a 64 bit Windows 10 PC Running AutoHotkey x32
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
GitHub: cocobelgica

Re: Insert obj to another obj without loop?

21 Mar 2015, 15:48

Yes, I would go with the loop as well. However, the Mergeable object above really shines if you want to merge an object with a large number of key-value pairs and at the same time it keeps the original object in pristine state.
User avatar
maestrith
Posts: 825
Joined: 16 Oct 2013, 13:52

Re: Insert obj to another obj without loop?

21 Mar 2015, 15:54

good to know :)
John H Wilson III 05/29/51 - 03/01/2020. You will be missed.AHK Studio OSDGUI Creator
Donations
Discord
All code is done on a 64 bit Windows 10 PC Running AutoHotkey x32

Return to “Ask For Help”

Who is online

Users browsing this forum: arczi_87, geoffh, Google [Bot], Green Astronaut, inseption86, mikeyww, one1tick, XMCQCX and 63 guests