我有一个Applescript记录列表,例如:
set mylist to {}
repeat with i from 1 to 8
set mylist to mylist & {{group:round (i / 3), info:i}}
end repeat
我希望按特定属性值过滤记录,以将记录列表拆分为:
{{{group:0, info:1}},
{{group:1, info:2}, {group:1, info:3}, {group:1, info:4}}
{{group:2, info:5}, {group:2, info:6}, {group:2, info:7}}
{{group:3, info:8}}}
我使用以下代码获取了我的过滤列表:
set group_values to {}
repeat with rec in mylist
if group of rec is not in group_values then
set group_values to group_values & (group of rec)
end if
end repeat
set r to {}
repeat with group_value in group_values
set by_group to {}
repeat with rec in mylist
if group of rec is equal to contents of group_value then
set by_group to by_group & {contents of rec}
end if
end repeat
set r to r & {by_group}
end repeat
我想知道是否有一种按属性值过滤记录的简化方法。
答案 0 :(得分:1)
这有点快......
set mylist to {}
repeat with i from 1 to 100
set mylist to mylist & {{group:round (i / 3), info:i}}
end repeat
set uniqueValues to {}
set filteredList to {}
repeat with aRecord in my mylist
set rGroup to aRecord's group
if rGroup is not in uniqueValues then
set end of uniqueValues to rGroup
set end of filteredList to {}
end if
set end of filteredList's item getOffset(rGroup, uniqueValues) to (aRecord's contents)
end repeat
on getOffset(pValue, searchList)
script o
property refList : searchList
end script
set searchListCount to count o's refList
repeat with i from 1 to searchListCount
if (item i of o's refList) = pValue then return i
end repeat
end getOffset