How can I find the intersections between multiple lists of names in apple script?

I want to compare multiple lists of names with apple script and have it spit out the names that are in all of the lists. I have been able to get it to compare two lists and give out the intersecting names, but I can't compare more than two lists at one time. Below is the script I have found online. However, I would like to compare names between multiple different lists. How can I make it compare multiple lists and give the common names between all of them?



set listA to {"red", "green", "blue"}

set listB to {"green", "blue"}


to intersection of listA against listB

local newList, a

set newList to {}

repeat with a in listA

set a to contents of a -- dereference implicit loop reference

if {a} is in listB then set end of newList to a

end repeat

newList

end intersection


intersection of listA against listB --> {"green", "blue"}

MacBook Air, OS X Yosemite (10.10.3), null

Posted on Oct 27, 2015 9:55 AM

Reply
3 replies

Oct 27, 2015 10:37 AM in response to Symonim

Here:


global listC

set listA to {"red", "green", "blue"}

set listB to {"green", "blue"}

set listC to {"green"}


to intersection of listA against listB

local newList, a

set newList to {}

repeat with a in listA

set a to contents of a -- dereference implicit loop reference

if {a} is in listB and {a} is in listC then set end of newList to a

end repeat

newList

end intersection


intersection of listA against listB


(135528)

Oct 27, 2015 1:05 PM in response to Symonim

Hello


Here're compact and efficient handlers you might try.



set a1 to {"red", "green", "blue"} set a2 to {"green", "blue"} set a3 to {"black", "yellow", "blue", "green"} set a4 to {"purple", "blue", "green", "green"} return {intersection({a1, a2, a3, a4}), union({a1, a2, a3, a4})} --> {{"green", "blue"}, {"red", "green", "blue", "black", "yellow", "purple"}} on intersection(argv) (* list argv : list of lists return list : intersection of lists in argv (duplicate element is removed) *) script o property x : {} property y : {} property z : {} on _intersect(a, b) set {x, y, z} to {a, b, {}} repeat with u in my x set u to u's contents if u is in my y and u is not in my z then set my z's end to u end repeat z end _intersect if argv = {} then return {} set result to argv's item 1 repeat with a in argv's rest _intersect(result, a's contents) end repeat end script tell o to run end intersection on union(argv) (* list argv : list of lists return list : union of lists in argv (duplicate element is removed) *) script o property x : {} property y : {} on _union(a, b) set {x, y} to {a & b, {}} repeat with u in my x set u to u's contents if u is not in my y then set my y's end to u end repeat y end _union if argv = {} then return {} set result to argv's item 1 repeat with a in argv's rest _union(result, a's contents) end repeat end script tell o to run end union



Cheers,

H

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How can I find the intersections between multiple lists of names in apple script?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.