Merging arrays question

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
sinkfaze
Posts: 616
Joined: 01 Oct 2013, 08:01

Merging arrays question

Post by sinkfaze » 28 Nov 2022, 11:08

I'm having trouble conceptualizing the method to accomplish this. I have two arrays, one with values only and one with arrays of values. I need to merge these into the array of values only in their intended sequential order. So for example:

Code: Select all

Arr1 :=	[1,4,7]
Arr2 :=	[[2,3],[5,6],[8,9,10]]
Arr1 should become:

Code: Select all

Arr1 :=	[1,2,3,4,5,6,7,8,9,10]
Just to clarify, I'm not asking how to push all of the data to Arr1 and sort it, the numbers are just stand ins for data that needs to be ordered in the new array. For example, the data at [8,9,10] in Arr2 is related to the data at 7 in Arr1, [5,6] to 4, etc.

My original thought was to reverse the order of Arr2 [[8,9,10],[5,6],[2,3]] and insert the values into Arr1 from back to front, but I'm stalling on that method. Any help is greatly appreciated.

teadrinker
Posts: 4331
Joined: 29 Mar 2015, 09:41
Contact:

Re: Merging arrays question

Post by teadrinker » 28 Nov 2022, 11:39

Why not like this:

Code: Select all

Arr1 :=	[1,4,7]
Arr2 :=	[[2,3],[5,6],[8,9,10]]

offset := 2
Loop % Arr1.Count() {
   Arr1.InsertAt(offset, Arr2[A_Index]*)
   offset += Arr2[A_Index].Count() + 1
}

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Merging arrays question

Post by swagfag » 28 Nov 2022, 11:43

Code: Select all

Arr1 :=	[1,4,7]
Arr2 :=	[[2,3],[5,6],[8,9,10]]

Arr3 := []
for i, e in Arr1
	Arr3.Push(e, Arr2[i]*)

Arr1 := Arr3

teadrinker
Posts: 4331
Joined: 29 Mar 2015, 09:41
Contact:

Re: Merging arrays question

Post by teadrinker » 28 Nov 2022, 11:45

swagfag wrote: Arr3 := []
What if arr 4GB ?

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Merging arrays question

Post by swagfag » 09 Dec 2022, 04:04

what if what

teadrinker
Posts: 4331
Joined: 29 Mar 2015, 09:41
Contact:

Re: Merging arrays question

Post by teadrinker » 09 Dec 2022, 04:14

You are creating an extra object, where it's unnecessary. This is a waste of memory, if arrays are large, it might matter.

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Merging arrays question

Post by swagfag » 09 Dec 2022, 04:32

u dont say.

i dont see "oh and keep the memory footprint low" anywhere in the requirements. why arent u deleting from Arr2 then?

teadrinker
Posts: 4331
Joined: 29 Mar 2015, 09:41
Contact:

Re: Merging arrays question

Post by teadrinker » 09 Dec 2022, 04:42

swagfag wrote: why arent u deleting from Arr2 then?
An initial object mustn't be changed unless this clearly required.
swagfag wrote: i dont see "oh and keep the memory footprint low" anywhere in the requirements
Well, in programming it's usually required by default. If there is an opportunity not to incur additional costs, it is better not to do this.

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Merging arrays question

Post by swagfag » 09 Dec 2022, 05:22

hear! hear! the Arbiter of Programming descends upon to enlighten us whats required, what isnt, whats necessary and what isnt, what should be done and what shouldnt!
An initial object mustn't be changed unless this clearly required.
says who? you? what happened to ur being overly concerned about memory?
Well, in programming it's usually required by default.
says who? you? and here i was thinking what was required in programming was to solve the problem
If there is an opportunity not to incur additional costs, it is better not to do this.
says who? you? how do u know its going to incur additional costs?

get off ur high horse already. only OP knows the requirements

teadrinker
Posts: 4331
Joined: 29 Mar 2015, 09:41
Contact:

Re: Merging arrays question

Post by teadrinker » 09 Dec 2022, 05:29

I think you need to calm down a bit.

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

Re: Merging arrays question

Post by Chunjee » 09 Dec 2022, 13:13

sinkfaze wrote:
28 Nov 2022, 11:08
Just to clarify, I'm not asking how to push all of the data to Arr1 and sort it, the numbers are just stand ins for data that needs to be ordered in the new array. For example, the data at [8,9,10] in Arr2 is related to the data at 7 in Arr1, [5,6] to 4, etc.

I call that a zip, not a merge. how I would do it:

Code: Select all

A := new biga() ; requires https://github.com/biga-ahk/biga.ahk

Arr1 := [1,4,7]
Arr2 := [[2,3],[5,6],[8,9,10]]

Arr3 := A.flattenDeep(A.zip(Arr1, Arr2))
; => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
https://biga-ahk.github.io/biga.ahk/#/?id=zip
https://biga-ahk.github.io/biga.ahk/#/?id=flattendeep

malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: Merging arrays question

Post by malcev » 11 Dec 2022, 13:17

teadrinker, compare:

Code: Select all

setbatchlines -1
loop 2
{
   Arr1 := [], Arr2 := []
   loop 100000
   {
      Arr1.Push(A_Index)
      Arr2.Push([A_Index, A_Index])
   }
   a := a_tickcount
   if (A_Index = 1)
   {
      offset := 2
      Loop % Arr1.Count() {
         Arr1.InsertAt(offset, Arr2[A_Index]*)
         offset += Arr2[A_Index].Count() + 1
      }
   }
   else
   {
      Arr3 := []
      for i, e in Arr1
         Arr3.Push(e, Arr2[i]*)
      Arr1 := Arr3
   }
   speed%A_Index% := a_tickcount - a
}
msgbox % speed1 "`n" speed2

teadrinker
Posts: 4331
Joined: 29 Mar 2015, 09:41
Contact:

Re: Merging arrays question

Post by teadrinker » 11 Dec 2022, 13:57

Yes, Push() is faster than InsertAt(), I don't say this is not true.

malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: Merging arrays question

Post by malcev » 11 Dec 2022, 16:32

Doesn`t that mean if arrays are large swagfag`s code is better?

teadrinker
Posts: 4331
Joined: 29 Mar 2015, 09:41
Contact:

Re: Merging arrays question

Post by teadrinker » 11 Dec 2022, 16:54

Not necessary. It depends what is in priority, speed, or memory consumption. It takes about one and a half times more memory.

teadrinker
Posts: 4331
Joined: 29 Mar 2015, 09:41
Contact:

Re: Merging arrays question

Post by teadrinker » 11 Dec 2022, 17:01

Code: Select all

setbatchlines -1
loop 2
{
   Arr1 := [], Arr2 := []
   loop 10000
   {
      Arr1.Push(A_Index)
      Arr2.Push([A_Index, A_Index, A_Index, A_Index, A_Index, A_Index, A_Index, A_Index, A_Index, A_Index])
   }
   a := a_tickcount
   if (A_Index = 1)
   {
      offset := 2
      Loop % Arr1.Count() {
         Arr1.InsertAt(offset, Arr2[A_Index]*)
         offset += Arr2[A_Index].Count() + 1
      }
   }
   else
   {
      Arr3 := []
      for i, e in Arr1
         Arr3.Push(e, Arr2[i]*)
      Arr1 := Arr3
   }
   speed%A_Index% := a_tickcount - a
}
msgbox % speed1 "`n" speed2

Post Reply

Return to “Ask for Help (v1)”