Merging two arrays (revisited)

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Simon Knight
Posts: 854
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

Merging two arrays (revisited)

Post by Simon Knight » Sat Apr 02, 2022 7:46 am

Hi,

I have the need to merge two arrays that both share the same structure and use numeric keys . I have a solution but wonder if there is a more efficient way of achieving the same result?

Code: Select all

on mouseUp pMouseButton
   --combine two arrays
   local tArrayA
   Local tArrayB
   Local tArrayC
   
   put "Cat" into tArrayA[2]["Species"]
   put "Dog" into tArrayA[4]["Species"]
   put "Mouse" into tArrayA[6]["Species"]
   put "Lion" into tArrayA[8]["Species"]
   put "Big" into tArrayA[8]["Size"]
   put "Wolf" into tArrayA[5]["Species"]
   
   
   put "Rose" into tArrayB[1]["Species"]
   put "Mallow" into tArrayB[2]["Species"]
   put "Wheat" into tArrayB[3]["Species"]
   put "Rape" into tArrayB[4]["Species"]
   put "Beetroot" into tArrayB[5]["Species"]
   
   put 0 into tCount
   repeat for each key tKey in tArrayA
      add 1 to tCount
      put tArrayA[tKey] into tArrayC[tCount]
   end repeat
   repeat for each key tKey in tArrayB
      add 1 to tCount
      put tArrayB[tKey] into tArrayC[tCount]
   end repeat
   
end mouseUp
best wishes
Skids

stam
Posts: 2679
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Merging two arrays (revisited)

Post by stam » Sat Apr 02, 2022 1:41 pm

As you're using 'repeat for each' loops that's likely to be really quite fast... not sure you can improve on that much!

just from a logistical point of view you could abstract it as a function, eg in the stack script:

Code: Select all

function combineArrays pArray1, pArray2
  local x, tArray
  repeat for each element tElement in pArray1
      add 1 to x -- as not previously used, initial value is 0 and this sets it to 1
      put tElement into tArray[x]
  end repeat

  repeat for each element tElement in pArray2
      add 1 to x -- as declared as function-wide will continue to increment
      put tElement into tArray[x]
   end repeat

   return tArray
end combineArrays

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9823
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Merging two arrays (revisited)

Post by FourthWorld » Sat Apr 02, 2022 5:26 pm

It's it necessary that the keys be sequential integers?

Are the species names unique?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

mtalluto
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 125
Joined: Tue Apr 11, 2006 7:02 pm
Location: Seattle, WA
Contact:

Re: Merging two arrays (revisited)

Post by mtalluto » Sat Apr 02, 2022 5:36 pm

Check out union in the dictionary.
Mark Talluto
--
Canela
design - develop - deploy: https://appli.io
Database and Cloud for LiveCode Developers: https://livecloud.io
Company: https://canelasoftware.com

stam
Posts: 2679
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Merging two arrays (revisited)

Post by stam » Sun Apr 03, 2022 12:17 am

mtalluto wrote:
Sat Apr 02, 2022 5:36 pm
Check out union in the dictionary.
Good to know about union Mark, I had no idea this command existed... but it won't do what the OP asks.
From the dictionary:
The union command combines targetArray and templateArray. Each key of targetArray is checked to see whether there is already an element with that key in templateArray. If there is, that element of targetArray is unchanged.
What the OP os asking for is concateting two arrays with identical keys but different values. Union will not change the values of existing keys.
FourthWorld wrote:
Sat Apr 02, 2022 5:26 pm
It's it necessary that the keys be sequential integers?
Are the species names unique?
We can only go by the OP:
Simon Knight wrote:
Sat Apr 02, 2022 7:46 am
I have the need to merge two arrays that both share the same structure and use numeric keys
This implies numerical keying... from what is the original poster notes and example he gives, the implication is that he is seeking to concatenate arrays.
Of course the eternal question is what the OP is trying to achieve and there may be a better way of doing things. Maybe.
But based on the info given i'd still give the same answer as above...

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9823
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Merging two arrays (revisited)

Post by FourthWorld » Sun Apr 03, 2022 1:47 am

stam wrote:
Sun Apr 03, 2022 12:17 am
Of course the eternal question is what the OP is trying to achieve and there may be a better way of doing things.
Hence my question for the OP.

The solution presented as functionally acceptable but merely slow maintains no consistent relationship between the integer and the associated species.

If the integer is arbitrary, need it be an integer?
If it can be a string, could he use the species name?

If the species name were equally acceptable as an arbitrary integer, that would both simplify the one-time merge and also reduce access time for every subsequent read and write by one hash.

But maybe the integer is important. We'll see.

Another question comes to mind: How many elements are in the arrays that performance became a noticeable issue?

Hundreds would be difficult to even measure.
Thousands would be difficult to notice.
Hundreds of thousands may merit a different structure.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Simon Knight
Posts: 854
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

Re: Merging two arrays (revisited)

Post by Simon Knight » Sun Apr 03, 2022 7:13 am

Interesting discussion. As for the high level keys they don't have to be numeric but it seems to make sense as it means I can inspect the contents in a datagrid by just setting the dgData. The array above is just a very simple example, the actual array has many more sub keys as can be seen in the screen shot taken of the IDE:
2022-04-01-093024-Screenshot 2022-04-01 at 09.30.19.png
When complete the code will populate the array with approximately 100,000 records.

While I think my original question is valid I have redesigned my code so that the array is passed between functions as a reference

Code: Select all

Function X @TheArray, pNextKey
along with the next key, meaning that there is no need to append one array to another as there is only one array. Doing this also means that the code is not duplicating large chunks of memory so is quicker to execute.

S
best wishes
Skids

Post Reply

Return to “Talking LiveCode”