How should I use clone() to prevent objects overwriting Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Mipha
Posts: 77
Joined: 27 Jul 2018, 17:08

How should I use clone() to prevent objects overwriting

02 Sep 2018, 12:01

Here's a very simplified example of a piece of code that I use

Code: Select all

value := {B:{C:[]}}
object1 := {A:{}}
object2 := {A:{}}
object1["A"] := value.clone()
object2["A"] := value.clone()
object1["A"]["B"]["C"] := object1["A"]["B"]["C"].clone()
object2["A"]["B"]["C"] := object2["A"]["B"]["C"].clone()
object1["A"]["B"]["C"].push("A")

msgbox % object1["A"]["B"]["C"][1] . ", " . object2["A"]["B"]["C"][1] ; < Both objects return A
Clone() doesn't seem to work in this example. And still fills both objects with the same variable. How should I solve this issue?
User avatar
Flipeador
Posts: 1204
Joined: 15 Nov 2014, 21:31
Location: Argentina
Contact:

Re: How should I use clone() to prevent objects overwriting  Topic is solved

02 Sep 2018, 12:11

I think you mean this:
https://autohotkey.com/boards/viewtopic ... 7&p=229509
https://autohotkey.com/boards/viewtopic.php?f=5&t=55014
https://autohotkey.com/boards/viewtopic ... 0&p=120134
https://autohotkey.com/boards/viewtopic ... 15&p=22699
https://autohotkey.com/boards/viewtopic ... 6&p=234076
(there are more related topics :crazy: )
edit* I just saw that the last link is from a topic of yours, maybe this is not what you're looking for. :think:

Code: Select all

value := {B:{C:[]}}
object1 := {A:{}}
object2 := {A:{}}
object1["A"] := ObjFullyClone( value )
object2["A"] := ObjFullyClone( value )
object1["A"]["B"]["C"] := ObjFullyClone( object1["A"]["B"]["C"] )
object2["A"]["B"]["C"] := ObjFullyClone( object2["A"]["B"]["C"] )
object1["A"]["B"]["C"].push("A")
object2["A"]["B"]["C"].push("B")  ; *

msgbox % object1["A"]["B"]["C"][1] . ", " . object2["A"]["B"]["C"][1] ; < Both objects return A

ObjFullyClone(obj)
{
    nobj := obj.Clone()
    for k,v in nobj
        if IsObject(v)
            nobj[k] := A_ThisFunc.(v)
    return nobj
} ; https://autohotkey.com/boards/viewtopic.php?f=74&t=28542&p=157603#p157603
Mipha
Posts: 77
Joined: 27 Jul 2018, 17:08

Re: How should I use clone() to prevent objects overwriting

02 Sep 2018, 12:21

Flipeador wrote:I think you mean this:
https://autohotkey.com/boards/viewtopic ... 7&p=229509
https://autohotkey.com/boards/viewtopic.php?f=5&t=55014
https://autohotkey.com/boards/viewtopic ... 0&p=120134
https://autohotkey.com/boards/viewtopic ... 15&p=22699
https://autohotkey.com/boards/viewtopic ... 6&p=234076
(there are more related topics :crazy: )
edit* I just saw that the last link is from a topic of yours, maybe this is not what you're looking for. :think:
Alright thanks. The objfullyclone() function ended up solving the problem.

Code: Select all

value := {B:{C:[]}}
object1 := {A:{}}
object2 := {A:{}}
object1["A"] := ObjFullyClone(value)
object2["A"] := ObjFullyClone(value)
object1["A"]["B"]["C"].push("A")

msgbox % object1["A"]["B"]["C"][1] . ", " . object2["A"]["B"]["C"][1] ; < returns A and empty string

ObjFullyClone(obj)
{
	nobj := obj.Clone()
	for k,v in nobj
		if IsObject(v)
			nobj[k] := A_ThisFunc.(v)
	return nobj
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: CodeKiller and 196 guests