• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Union of sets (lists
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Union of sets (lists


  • Subject: Re: Union of sets (lists
  • From: Paul Berkowitz <email@hidden>
  • Date: Sat, 11 Jan 2003 16:58:18 -0800

On 1/11/03 3:49 PM, "Michelle Steiner" <email@hidden> wrote:

> set SetA to {"a", "b"} -- elements for set A
> set SetB to {"c", "d", "e"} -- elements for set B and so on....
> set foo to union(SetA, SetB)
>
> to union(a, b)
> if (class of a is not list) or (class of b is not list) then
> display dialog "Only lists can be used." buttons {"Cancel"}
> default button 1
> end if
>
> if (count (a)) is greater than (count (b)) then
> copy a to x
> copy b to y
> else
> copy b to x
> copy a to y
> end if
> set FinalResult to y
> repeat with i in x
> if (i is not in y) and (i is not in FinalResult) then
> copy i to end of FinalResult
> end if
> end repeat
> end union
>
> But with Script Editor 2.0 beta, the result is:
> {"a", "b", item 1 of {"c", "d", "e"}, item 2 of {"c", "d", "e"}, item 3
> of {"c", "d", "e"}}
>
> And with Script Editor 1.9, the result is:
> item 3 of {"c", "d", "e"}
>
> What is going wrong here?


1) Really, it's simpler just to _never_ use 'repeat with i in x'
constructions. If you always use

repeat with i from 1 to (count x)
set anItem to item i of x

you'll be much happier. Otherwise, AppleScript will throw a reference ( item
1 of {"c", "d", "e"} ) into the mix, rather than evaluating it, and the
rigor of the equality prevents that from being equal to "c".



2) Also it's a bit sloppy to use
if (i is not in y)


when i is a different class from y (see ASLG 'contains' operator). It should
be

if ({i} is not in y)

but it will coerce OK when i is a string and y is a list; that's not your
problem here.

3) So if you do:

set SetA to {"a", "b"} -- elements for set A
set SetB to {"c", "d", "e"} -- elements for set B and so on....
set foo to union(SetA, SetB)

to union(a, b)
if (class of a is not list) or (class of b is not list) then
display dialog "Only lists can be used." buttons {"Cancel"} default
button 1
end if

if (count (a)) is greater than (count (b)) then
copy a to x
copy b to y
else
copy b to x
copy a to y
end if
set FinalResult to y
repeat with i from 1 to (count x)
set anItem to item i of x
if ({anItem} is not in y) and ({anItem} is not in FinalResult) then
copy anItem to end of FinalResult
end if
end repeat
end union

--> "e"

rather than the muddle you got. This is in SE 2.0 beta, or any other script
editor. [This is the equivalent as

item 3 of {"c", "d", "e"}

for your original 'i in x' version in SE 1.9.]


4) So now you see that your script was wrong to begin with: that's no union
of SetA and SetB. That's because your handler doesn't return the result
you're looking for. All you're getting us the result of the last command
carried out:

copy anItem to end of FinalResult

You need to add

return FinalResult

just before 'end union'. Then you get

--> {"a", "b", "c", "d", "e"}


SE 2.0 beta (or anywhere) . [The version you got:

{"a", "b", item 1 of {"c", "d", "e"}, item 2 of {"c", "d", "e"}, item 3 of
{"c", "d", "e"}}

must have had 'return FinalResult' in it; that's what your original version
gives with the line included.)

--
Paul Berkowitz
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.

References: 
 >Union of sets (lists (From: Michelle Steiner <email@hidden>)

  • Prev by Date: Re: Union of sets (lists
  • Next by Date: [OFF] ... sort of ...
  • Previous by thread: Re: Union of sets (lists
  • Next by thread: Re: Union of sets (lists
  • Index(es):
    • Date
    • Thread