Hi,
I would like to know if there is a difference between the following two statements:
x:={a:1, b:2}
and
x:={a:1, b:2, c:""}
Question about objects Topic is solved
Re: Question about objects Topic is solved
One has two keys and the other has three. While the "c" key is blank-like, it does make them different.
https://biga-ahk.github.io/biga.ahk/#/?id=isequal
Code: Select all
A := new biga() ; requires https://www.npmjs.com/package/biga.ahk
msgbox, % A.isEqual({a:1, b:2}, {a:1, b:2, c:""})
; => false
msgbox, % {a:1, b:2}.count()
; => 2
msgbox, % {a:1, b:2, c:""}.count()
; => 3
https://biga-ahk.github.io/biga.ahk/#/?id=isequal
-
- Posts: 4331
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: Question about objects
Code: Select all
MsgBox % CompareArrays({a:1, b:2}, {a:1, b:2, c:""})
CompareArrays(obj1, obj2) {
Loop 2 {
VarSetCapacity(Bin%A_Index%, Size%A_Index% := RawObjectSize(obj%A_Index%, 0) + 8, 0)
RawObject(obj%A_Index%, NumPut(Size%A_Index% - 8, &Bin%A_Index%, "Int64"), 0)
}
Return Size1 = Size2 && !DllCall("msvcrt\memcmp", "Ptr", &Bin1, "Ptr", &Bin2, "Ptr", Size1, "Cdecl")
}
RawObject(obj, addr, buf := 0, objects := 0) {
; Type.Enum: Char.1 UChar.2 Short.3 UShort.4 Int.5 UInt.6 Int64.7 UInt64.8 Double.9 String.10 Object.11
; Negative for keys and positive for values
if !objects
objects:={(""):0,(obj):0}
else
objects[obj]:=(++objects[""])
for k, v in obj
{ ; 9 = Int64 for size and Char for type
if IsObject(k) {
if objects.HasKey(k)
NumPut(-12,addr+0,"Char"),NumPut(objects[k],addr+1,"Int64"),addr+=9
else
NumPut(-11,addr+0,"Char"),NumPut(sz:=RawObjectSize(k,buf),addr+1,"Int64"),RawObject(k,addr+9,buf,objects),addr+=sz+9
} else
NumPut(-10,addr+0,"Char"),NumPut(sz:=StrPut(k,addr+9)*2,addr+1,"Int64"),addr+=sz+9
if IsObject(v) {
if objects.HasKey(v)
NumPut(12,addr+0,"Char"),NumPut(objects[v],addr+1,"Int64"),addr+=9
else
NumPut(11,addr+0,"Char"),NumPut(sz:=RawObjectSize(v,buf),addr+1,"Int64"),RawObject(v,addr+9,buf,objects),addr+=sz+9
} else {
NumPut(10,addr+0,"Char"),NumPut(sz:=buf?obj.GetCapacity(k):StrPut(v)*2,addr+1,"Int64")
DllCall("RtlMoveMemory","PTR",addr+9,"PTR",buf?obj.GetAddress(k):&v,"PTR",sz),addr+=sz+9
}
}
}
RawObjectSize(obj,buf:=0,objects:=0){
if !objects
objects:={(obj):1}
else if !objects.HasKey(obj)
objects[obj]:=1
for k, v in obj
{
if IsObject(k)
sz+=objects.HasKey(k)?9:RawObjectSize(k,buf,objects)+9
else
sz+=StrPut(k)*2+9
if IsObject(v)
sz+=objects.HasKey(v)?9:RawObjectSize(v,buf,objects)+9
else
sz+=(buf?obj.GetCapacity(k):StrPut(v)*2)+9
}
Return sz
}
Re: Question about objects
A practical way in which you'd see the difference show up is when you loop through all the elements of the objects:
Code: Select all
x:={a:1, b:2}
for k, v in x
MsgBox, % k ": " v
x:={a:1, b:2, c:""}
for k, v in x
MsgBox, % k ": " v
-
- Posts: 4331
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: Question about objects
@boiler
Try compare these:
Try compare these:
Code: Select all
a := {a:{a:"a", b: {a:"a", b: "b"}}, b: {a:"a", b: "b"}}
b := {a:{a:"a", b: {a:"a", b: "c"}}, b: {a:"a", b: "b"}}
Re: Question about objects
@teadrinker - I am not saying your approach is not a more complete comparison for the general case and my post would be. And that does nothing to diminish the point of my post. As I mentioned, what I showed was a practical difference OP would see with the actual arrays mentioned. It would help him see why the difference between the two arrays might be meaningful to him in a way that a function telling him they are different would not necessarily do.
-
- Posts: 4331
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: Question about objects
Perhaps. How does that make the demonstration of a tangible difference between those two arrays any less valuable? It shows how a script’s actual operation would be different with one array vs. the other. The OP would see that the actual number of times the for loop would be executed is different. Just seeing that a comparison function reports that they are different may leaving him thinking, “So what? Is that going to have any impact on my script?”
-
- Posts: 4331
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: Question about objects
What if the actual number of times the for loop is the same? What does it mean?teadrinker wrote: ↑I suppose, those were just examples.
Re: Question about objects
Why are you asking about a hypothetical when I already stated that I was showing him how in this case it would make a difference in how his script would operate if he looped through its keys and I acknowledged it doesn't address the general case? Don't answer that question. It was rhetorical and I am tired of this pointless discussion.
Re: Question about objects
@all
Thank you, I see the difference. I was not sure what would happen. I could have thought of looking at array.count() by myself to see that a key/value pair is stored even when the value is empty.
Thank you, I see the difference. I was not sure what would happen. I could have thought of looking at array.count() by myself to see that a key/value pair is stored even when the value is empty.
Who is online
Users browsing this forum: Descolada, Google [Bot] and 247 guests