.Push( Object2* ): Adding an object as array of values to an object

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
hotkeyguy
Posts: 170
Joined: 11 Oct 2014, 12:22

.Push( Object2* ): Adding an object as array of values to an object

22 Nov 2019, 07:25

Hello,

two objects used as arrays, one added as array of values to the other object:
Object1 := { 1: "a", 2: "b", 3: "c" }
Object2 := { 4: "d", 5: "e", 6: "f" }
Object1.Push( Object2* )

The result is:
Object[1] = "a"
Object[2] = "b"
Object[3] = "c"
Object[7] = "d" ; expected [4]
Object[8] = "e" ; expected [5]
Object[9] = "f" ; expected [6]


Can somebody explain that gap in the indexes?

Same result:
Object1 := { 1: "a", 2: "b", 3: "c" }
IdxInsert := Object1.MaxIndex()
Object2 := { 4: "d", 5: "e", 6: "f" }
Object1.InsertAt( IdxInsert + 1, Object2* )



Many thanks and greetings
hotkeyguy
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: .Push( Object2* ): Adding an object as array of values to an object

22 Nov 2019, 08:38

variadic function calls wrote: Numbering of parameters within the source array begins at 1.
Optional parameters may be entirely omitted from the array.
insertat wrote: The position to insert Value1 at. Subsequent values are inserted at Pos+1, Pos+2, etc
Push behaves in a similar fashion but the wording in the docs is a little off.

Cheers.
User avatar
Chunjee
Posts: 1422
Joined: 18 Apr 2014, 19:05
Contact:

Re: .Push( Object2* ): Adding an object as array of values to an object

18 Jan 2020, 23:52

Sorry for necro but thread caught my interest.

.concat may be of interest to OP

Code: Select all

A := new biga()

Object1 := { 1: "a", 2: "b", 3: "c" }
Object2 := { 4: "d", 5: "e", 6: "f" }
msgbox, % A.printObj(A.concat(Object1, Object2))
; => {1:"a", 2:"b", 3:"c", 4:"", 5:"", 6:"", 7:"d", 8:"e", 9:"f"}
ExitApp
Not sure I understand the use case ahk was trying to serve, I was expecting to find the OP was wrong but it really does start pushing at Object2.MaxIndex + 1 (which in this case is 7)
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: .Push( Object2* ): Adding an object as array of values to an object

19 Jan 2020, 01:44

Chunjee wrote:
18 Jan 2020, 23:52
Not sure I understand the use case ahk was trying to serve, I was expecting to find the OP was wrong
Hi. Note the quote above,
Numbering of parameters within the source array begins at 1.
Optional parameters may be entirely omitted from the array.
which gives the intuitive (and much more useful) behaviour that f([, 2, 3]*) behaves as f(, 2, 3) rather than f(2,3).

Cheers.
guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: .Push( Object2* ): Adding an object as array of values to an object

19 Jan 2020, 11:25

Chunjee wrote:
18 Jan 2020, 23:52

Code: Select all

Object1 := { 1: "a", 2: "b", 3: "c" }
Object2 := { 4: "d", 5: "e", 6: "f" }
Not sure I understand the use case ahk was trying to serve, I was expecting to find the OP was wrong but it really does start pushing at Object2.MaxIndex + 1 (which in this case is 7)
no, it starts pushing at Object1.MaxIndex+1

the confusion is because the definition of obj2 starts at element 4 and omits its initial 1,2,3 array indexes. here's what the two arrays really look like:

obj1 = ["a", "b", "c"]
obj2 = ["", "", "", "d", "e", "f"]

now its obvious what happens when you push to append/concat obj2 onto 1

if you did this:

Object1 := { 1: "a", 2: "b", 3: "c" }
Object2 := { 1: "d", 2: "e", 3: "f" }

then you'd get what you expect

User avatar
Chunjee
Posts: 1422
Joined: 18 Apr 2014, 19:05
Contact:

Re: .Push( Object2* ): Adding an object as array of values to an object

19 Jan 2020, 11:52

guest3456 wrote:
19 Jan 2020, 11:25
the confusion is because the definition of obj2 starts at element 4 and omits its initial 1,2,3 array indexes. here's what the two arrays really look like:

obj1 = ["a", "b", "c"]
obj2 = ["", "", "", "d", "e", "f"]

now its obvious what happens when you push to append/concat obj2 onto 1

if you did this:

Object1 := { 1: "a", 2: "b", 3: "c" }
Object2 := { 1: "d", 2: "e", 3: "f" }

then you'd get what you expect
Thank you that makes sense.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: .Push( Object2* ): Adding an object as array of values to an object

19 Jan 2020, 11:53

obj1 = ["a", "b", "c"]
obj2 = ["", "", "", "d", "e", "f"]
Note that there is a difference between ["", x] and [, x], where the former has two values, and the latter only one. If you concatenate obj1 and obj2 you'll notice if you ask haskey/count or enumerate. But it is a good visual.

Cheers.
User avatar
Chunjee
Posts: 1422
Joined: 18 Apr 2014, 19:05
Contact:

Re: .Push( Object2* ): Adding an object as array of values to an object

19 Jan 2020, 11:59

I'm noticing that the answer was staring me straight in the face in my own code:
; => {1:"a", 2:"b", 3:"c", 4:"", 5:"", 6:"", 7:"d", 8:"e", 9:"f"}
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: .Push( Object2* ): Adding an object as array of values to an object

19 Jan 2020, 12:05

Hehe :thumbup:. Just note that the op's result is {1:"a", 2:"b", 3:"c", 7:"d", 8:"e", 9:"f"}.

Cheer.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: badkarma5150, Spawnova and 293 guests