2012年11月1日木曜日

【AppleScript】リストから該当ナンバーのアイテムを削除

アイテムリストから指定したナンバーのアイテムを削除するハンドラです。
分かりづらいので補足すると、
{"あ","い","う","え","お"}と{"1","3","5"}を入れると、
{"い","え"}が返ってきます。
ナンバーは配列でも単数でも動きます。逆に、該当ナンバーのアイテムを抽出するのは、
set 返す配列名 to item number ナンバー of 配列名
とかに書き換えるといいでしょう。(言わずもがなですが・・・)

トロくさいのでa reference toで高速化を試みましたが、あまり変わらないという・・・
100倍速くする実力をもっているはずなのに、なんでなんだろう・・・


--リストから該当するナンバーのアイテムを削除
--ナンバーがリストでも動作します
on delete_list(theList, Num)
set theList_ref to theList
-- set theList to a reference to theList --なぜか高速化しない
if (count of Num) = 1 then
set theList to delete_item(theList, Num)
else
set i to 0
repeat with a in Num
set a to a - i
set theList to delete_item(theList, a)
set i to i + 1
end repeat
return theList
end if
end delete_list

on delete_item(theList, Num)
if Num = 1 then
set theList to contents of items 2 thru -1 of theList
else if Num = (count of theList) then
set theList to contents of items 1 thru -2 of theList
else
set alist to contents of items 1 thru (Num - 1) of theList
set blist to contents of items (Num + 1) thru (count of theList) of theList
set theList to alist & blist
end if

return theList
end delete_item

0 件のコメント:

コメントを投稿