Page 1 of 1

Can't figure out "union"

Posted: Tue Sep 21, 2010 9:46 pm
by professorhagen
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.

Re: Can't figure out "union"

Posted: Wed Sep 22, 2010 8:15 am
by Mark
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

Re: Can't figure out "union"

Posted: Wed Sep 22, 2010 11:20 pm
by bn
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