tell application "Finder"
set t to {1, 1, 2, 3, 4, 5, 6, 7, 2, 98, 34, 78, 7}
-- here is want to count number of 1's in the list "t" without using repeat loop
end tell
What is need here is, I want to count number of 1’s in the list t without using repeat-end loop. Is this possible.
I’m not positive if this works in every case although I can’t think of one where it doesn’t…
set numberToCount to 1
set t to {1, 1, 2, 3, 4, 5, 6, 1, 2, 1, 34, 78, 7}
set text item delimiters to space
set a to t as text
set text item delimiters to numberToCount as text
set b to text items of a
set text item delimiters to ""
set countOfNumber to (count of b) - 1
--> 4
set t to {1, 1, 2, 3, 4, 5, 6, 7, 2, 98, 34, 78, 7}
set countVal to 1
set {tid, text item delimiters} to {text item delimiters, ","}
set theCount to do shell script "php -r '$array = array(" & (t as Unicode text) & "); $values=array_count_values($array); echo $values[" & countVal & "];'"
set text item delimiters to tid
But, Hank, consider that your method will count all 1’s, not just those that stand alone in the original list:
set numberToCount to 1
set t to {1, 1, 2, 3, 4, 51, 6, 1, 2, 1, 34, 121, 7}
set text item delimiters to space
set a to t as text
set text item delimiters to numberToCount as text
set b to text items of a
set text item delimiters to ""
set countOfNumber to (count of b) - 1
--> 7
Ha! Good catch Adam. I thought I took care of that. This one takes care of it. It just needed a couple more spaces…
set numberToCount to 1
set t to {1, 1, 2, 3, 4, 51, 6, 1, 2, 1, 34, 121, 1}
set text item delimiters to space & space
set a to space & (t as text) & space
set text item delimiters to (space & (numberToCount as text) & space)
set b to text items of a
set text item delimiters to ""
set countOfNumber to (count of b) - 1
--> 7