Фильтровать записи Applescript по значению свойства

У меня есть список записей 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

Я хотел бы знать, есть ли упрощенный способ фильтрации записей по значениям свойств.


person msampaio    schedule 11.12.2014    source источник


Ответы (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
person adayzdone    schedule 11.12.2014