AppleScript - 比较包含其他列表的任何部分的作品的列表

时间:2013-01-31 00:15:11

标签: list applescript compare

输入:

set firstList to {'red ball','blue','yellow'}
set secondList to {'grasshopper','yellowjacket','blueberry','redball'}

输出:

{'yellowjacket','blueberry','redball'}

完成这样的事情的最佳方法是什么?基本上我正在寻找每个列表中的项目之间的任何相似性。

1 个答案:

答案 0 :(得分:1)

尝试:

set matchList to {}
set firstList to {"red ball ", "blue", "yellow"}
set secondList to {"grasshopper", "yellowjacket", "blueberry", "redball"}

repeat with aItem in firstList
    set myTerm to do shell script "echo " & quoted form of aItem & " | sed -E 's/  */ ?/g'"
    repeat with bItem in secondList
        try
            set end of matchList to (do shell script "echo " & quoted form of bItem & " | grep -E " & quoted form of myTerm)
        end try
    end repeat
end repeat

return matchList

修改

    set matchList to {}
set firstList to {"jamesdoe","jackknow","henryrod"}
set secondList to {"James Doe", "Jack Know", "John Matthews"}

set {TID, text item delimiters} to {text item delimiters, {""}}
repeat with aItem in firstList
    set AppleScript's text item delimiters to {""}
    set myTerm to text items of aItem
    set AppleScript's text item delimiters to {" ?"}
    set myTerm to myTerm as text
    repeat with bItem in secondList
        try
            set end of matchList to (do shell script "echo " & quoted form of bItem & " | grep -Ei " & quoted form of myTerm)
        end try
    end repeat
end repeat

set text item delimiters to TID
return matchList

编辑2

set matchList to {}
set firstList to {"jamesdoe", "jackknow", "henryrod"}
set secondList to {"James Doe", "Jack Know", "John Matthews"}

set {TID, text item delimiters} to {text item delimiters, {""}}
repeat with aItem in firstList
    set AppleScript's text item delimiters to {""}
    set myTerm to text items of aItem
    set AppleScript's text item delimiters to {" ?"}
    set myTerm to myTerm as text
    repeat with bItem in secondList
        try
            do shell script "echo " & quoted form of bItem & " | grep -Ei " & quoted form of myTerm
            set end of matchList to (aItem's contents)
            exit repeat
        end try
    end repeat
end repeat

set text item delimiters to TID
return matchList