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
.Push( Object2* ): Adding an object as array of values to an object
Re: .Push( Object2* ): Adding an object as array of values to an object
variadic function calls wrote: Numbering of parameters within the source array begins at 1.
Optional parameters may be entirely omitted from the array.
Push behaves in a similar fashion but the wording in the docs is a little off.insertat wrote: The position to insert Value1 at. Subsequent values are inserted at Pos+1, Pos+2, etc
Cheers.
Re: .Push( Object2* ): Adding an object as array of values to an object
Sorry for necro but thread caught my interest.
.concat may be of interest to OP
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)
.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
Re: .Push( Object2* ): Adding an object as array of values to an object
Hi. Note the quote above,
which gives the intuitive (and much more useful) behaviour that f([, 2, 3]*) behaves as f(, 2, 3) rather than f(2,3).Numbering of parameters within the source array begins at 1.
Optional parameters may be entirely omitted from the array.
Cheers.
Re: .Push( Object2* ): Adding an object as array of values to an object
no, it starts pushing at Object1.MaxIndex+1Chunjee wrote: ↑18 Jan 2020, 23:52Not 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)Code: Select all
Object1 := { 1: "a", 2: "b", 3: "c" } Object2 := { 4: "d", 5: "e", 6: "f" }
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
Re: .Push( Object2* ): Adding an object as array of values to an object
Thank you that makes sense.guest3456 wrote: ↑19 Jan 2020, 11:25the 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
Re: .Push( Object2* ): Adding an object as array of values to an object
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.obj1 = ["a", "b", "c"]
obj2 = ["", "", "", "d", "e", "f"]
Cheers.
Re: .Push( Object2* ): Adding an object as array of values to an object
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"}
; => {1:"a", 2:"b", 3:"c", 4:"", 5:"", 6:"", 7:"d", 8:"e", 9:"f"}
Re: .Push( Object2* ): Adding an object as array of values to an object
Hehe . Just note that the op's result is {1:"a", 2:"b", 3:"c", 7:"d", 8:"e", 9:"f"}.
Cheer.
Cheer.
Who is online
Users browsing this forum: badkarma5150, Spawnova and 293 guests