Re: Union of sets (lists
Re: Union of sets (lists
- Subject: Re: Union of sets (lists
- From: Deivy Petrescu <email@hidden>
- Date: Sat, 11 Jan 2003 22:57:10 -0500
At 16:49 -0700 1/11/03, Michelle Steiner 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?
--Michelle
"There's some good in the world, Mr. Frodo, and it's worth fighting for."
Michelle,
First, I get "item 3 of {"c", "d", "e"}" running AS 2.0
I do not understand if you are asking why item 3 of ... instead of
"e", for instance. I believe you know your i is not the contents of
item i of y (or x).
However, if you allow me, there is a problem with the code. If
someone sends you a "wrong set" your code returns the wrong union.
for instance :
set SetA to {"a", "a","a","a"} -- elements for set A
set SetB to {"c", "d", "e"} -- elements for set B and so on....
set foo to union(SetA, SetB)
--->union = {"a", "a","a","a","c", "d", "e"}.
So I suggest the following:
_______________________________________
set SetA to {"a", "a","a","a"} -- elements for set A
set SetB to {"c", "d", "e"} -- elements for set B and so on....
return 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
set munion to {}
repeat with i in b
set b to rest of b
log b
if (contents of i is not in a) and (contents of i is
not in b) then set end of munion to (contents of i)
log munion
end repeat
repeat with i in a
set a to rest of a
log b
if (contents of i is not in a) and (contents of i is
not in b) then set end of munion to (contents of i)
log munion
end repeat
return munion
end union
--->{"c", "d", "e", "a"}
Regards
--
Deivy Petrescu
http://www.dicas.com
_______________________________________________
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.