Question about objects Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
braunbaer
Posts: 478
Joined: 22 Feb 2016, 10:49

Question about objects

11 May 2021, 14:50

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:""}
User avatar
Chunjee
Posts: 1418
Joined: 18 Apr 2014, 19:05
Contact:

Re: Question about objects  Topic is solved

11 May 2021, 14:58

One has two keys and the other has three. While the "c" key is blank-like, it does make them different.

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
teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: Question about objects

11 May 2021, 16:15

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
}
User avatar
boiler
Posts: 16911
Joined: 21 Dec 2014, 02:44

Re: Question about objects

11 May 2021, 16:31

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
teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: Question about objects

11 May 2021, 16:37

@boiler
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"}}
User avatar
boiler
Posts: 16911
Joined: 21 Dec 2014, 02:44

Re: Question about objects

11 May 2021, 16:44

@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.
teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: Question about objects

11 May 2021, 16:54

boiler wrote: with the actual arrays mentioned
I suppose, those were just examples. (?)
User avatar
boiler
Posts: 16911
Joined: 21 Dec 2014, 02:44

Re: Question about objects

11 May 2021, 18:36

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?”
teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: Question about objects

11 May 2021, 19:00

boiler wrote: The OP would see that the actual number of times the for loop would be executed is different.
teadrinker wrote: I suppose, those were just examples.
What if the actual number of times the for loop is the same? What does it mean?
User avatar
boiler
Posts: 16911
Joined: 21 Dec 2014, 02:44

Re: Question about objects

11 May 2021, 19:38

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.
braunbaer
Posts: 478
Joined: 22 Feb 2016, 10:49

Re: Question about objects

11 May 2021, 21:06

@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.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], Joey5, TAC109 and 177 guests