Can't figure out "union"

Creating desktop or client-server database solutions?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
professorhagen
Posts: 9
Joined: Wed Feb 21, 2007 12:18 am
Contact:

Can't figure out "union"

Post by professorhagen » Tue Sep 21, 2010 9:46 pm

I am having no luck with the union command. The documentation says

Examples:
union monthlyPayments with quarterlyPayments
Use the union command to combine two arrays, eliminating duplicate elements

I know the variables, are arrays, but what do they actually look like? Can anyone give me a simple sample script I can copy and paste into, say, a button to put the get union of two arrays, just as an illustration? I tried with

union array with testArray

where array="a,b,c,d" and testArray="c,d,e,f", hoping that the union command put yield "a,b,c,d,e,f". Obviously I am way off. Thanks in advance.

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: Can't figure out "union"

Post by Mark » Wed Sep 22, 2010 8:15 am

Hagen,

"a,b,c,d" is not an array. It is a string containing characters delimited by commas. If you want to turn this into an array, you can do this:

Code: Select all

on mouseUp
     put "a,b,c,d,e" into x
     split x by comma
     put the keys of x
end mouseUp
This produces an array with 5 keys. The keys are displayed in the message box.

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4163
Joined: Sun Jan 07, 2007 9:12 pm

Re: Can't figure out "union"

Post by bn » Wed Sep 22, 2010 11:20 pm

professorhagen,
try this in a button.

Code: Select all

on mouseUp
   put "1,2,3,4,5,6" into tItemList
   put "a,b,c,d" into tKeyList1
   put "e,f,g,h" into tKeyList2
   -- create first array
   repeat with i = 1 to 4
      put any item of tItemlist into tArray1[item i of tKeyList1]
   end repeat
   -- create second array for the union
   repeat with i = 1 to 4
      put any item of tItemList into tTestArray[item i of tKeyList2]
   end repeat
   union tArray1 with tTestArray
   put the keys of tArray1 into tTheKeys
   
   -- remember the keys of an array are not sorted
   sort tTheKeys
   put tTheKeys
end mouseUp
it will put the union of the keys of the two arrays into the message box:
a
b
c
d
e
f
g
h
regards
Bernd

Post Reply