I’m sure you can use the ‘whose’ line on list to count items thats are of a choosen value, I’ve tried may lines, can someone tell me this.
set TheList to {true, false, true, false, false}
set TheCount to count of (items of TheList whose it is false) --error
or not counting just getting a smaller list
set TheWordList to {"false", "true", "false", "false"}
set TheWordsThatAreFalse to items in TheWordList whose it contains "false" -- error
Syntax is
referenceToObject ( whose | where ) Boolean
Is it becuase I’m sending a list to the whoose line?
I can work around it by using a repeat but that not nice applescripting
set TheList to {true, false, true, false, false}
set TheCount to 0
repeat with i from 1 to (count of TheList)
if item i of TheList is false then set TheCount to TheCount + 1
end repeat
Unfortunately, you cannot extract/examine lists/records using the containment commands. This post has some good explanations, especially #6 through #8.
set TheWordList to {"false", "true", "false", "false"}
set t to TheWordList as string
set utid to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"false"}
set temp_list to text items of t
set AppleScript's text item delimiters to utid
set c to (count temp_list) - 1
So I wasn’t going mad. Thanks Adam Bell thans a little short.
Would loading into a script object speed it up?
set L to {true, false, true, false, false}
Fast(L)
on Fast(L)
script C
property SL : L
end script
set CF to 0
repeat with A in C's SL
if not A then set CF to CF + 1
end repeat
return CF
end Fast
Sure does: here’s the comparison on my machine using Nigel Garvey’s “Lotsa”; a little over 10 to 1 on a large array, but hardly worth the trouble for a small one.
set Timings to lotsa(1000)
on lotsa(many)
-- Any other preliminary values here.
set L1 to {true, false, true, false, false}
set L to {}
repeat 50 times
set L to L & L1
end repeat
-- Dummy loop to absorb a small observed
-- time handicap in the first repeat.
repeat many times
end repeat
-- Test 1.
set t to GetMilliSec
repeat many times
-- First test code or handler call here.
script C
property SL : L
end script
set CF to 0
repeat with A in C's SL
if not A then set CF to CF + 1
end repeat
end repeat
set t1 to ((GetMilliSec) - t) / 1000
-- Test 2.
set t to GetMilliSec
repeat many times
-- Second test code or handler call here.
set CF to 0
repeat with i in L
if not i then set CF to CF + 1
end repeat
end repeat
set t2 to ((GetMilliSec) - t) / 1000
-- Timings.
return {t1, t2, t1 / t2, t2 / t1}
--> on my machine: {0.951, 10.022, 0.094891239274, 10.538380651945}
end lotsa