2012年10月26日金曜日

【AppleScript】 2つのリストから重複するアイテムを抜き出す

2つのリストに重複するアイテムを返すハンドラです。
重複はファイル名などの文字列で考えてます。as textなどしたほうが確実かも。

{duplication_word:{重複アイテム},a_num_list:{aリストの該当ナンバー}, b_num_list:{bリストの該当ナンバー}}
を返します。
"item number i of duplication_word of ほにゃらら" みたいな使い方を想定しています。
二重リスト(?)はrepeatで扱いやすくて便利ですね。

リストを総当たりでチェックするので、アイテム数が多いととても時間がかかります。
スペック次第ですが、数百あたりからアヤシイです。使う時は気をつけてください。




set a to {"あ", "い", "う", "え"}
set b to {"う", "え", "お"}
my duplicate_check(a, b)
-- → {duplication_word:{item 3 of {"あ", "い", "う", "え"}, item 4 of {"あ", "い", "う", "え"}}, a_num_list:{3, 4}, b_num_list:{1, 2}}

(*
2つのリストから重複するアイテムを抜き出す
return:{duplication_word:{重複アイテム},a_num_list:{alistの該当ナンバー}, b_num_list:{blistの該当ナンバー}}
*)
on duplicate_check(alist, blist)

set duplication_word to {}
set a_num_list to {}
set b_num_list to {}
set c to {duplication_word:{}, a_num_list:{}, b_num_list:{}}

set a_num to 0
set b_num to 0

repeat with n in alist
set a_num to a_num + 1
repeat with m in blist
set b_num to b_num + 1
if n contains m then --なぜか=だと動かない
set end of duplication_word to n
set end of a_num_list to a_num
set end of b_num_list to b_num
end if
end repeat
set b_num to 0
end repeat

set duplication_word of c to duplication_word
set a_num_list of c to a_num_list
set b_num_list of c to b_num_list
return c

end duplicate_check

0 件のコメント:

コメントを投稿