2014-12-11 1 views
0

나는 같은 AppleScript로 레코드 목록이 있습니다필터 AppleScript를 기록

:

set mylist to {} 
repeat with i from 1 to 8 
    set mylist to mylist & {{group:round (i/3), info:i}} 
end repeat 

내가 좋아하는 뭔가 기록 목록을 분할 특정 속성 값으로 레코드를 필터링하고 싶습니다를

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 

은 내가이 있는지 알고 싶습니다

{{{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}}} 
은이 코드 내 필터링 된 목록을 가지고 속성 값별로 레코드를 필터링하는 간단한 방법. 이것은 조금 빠른

답변

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