each
- keyword -- repeat with each, each expressions
- special variable in "sort by" and "where" clause of each expressions
- each expressions
- An each expression works with each item of a list or iterator, or each chunk (character, word, line, or item) of a text value, and produces a new list of values. Any expression of the form "each chunkType of sourceValue" will yield a list containing all of the chunks of that type:
put each character of "Sweet!" -- ("S","w","e","e","t","!")
put each word of "Wisdom begins in wonder"
-- ("Wisdom","begins","in","wonder")
More interestingly, an each expression can be part of a larger expression:
put "Z" & each character of "Cat" -- ("ZC","Za","Zt")
put 2 + each item of "1,2,5,6" -- (3,4,7,8)
put the length of each word in "Wisdom begins in wonder"
-- (6,6,2,6)
put each word of "Wisdom begins in wonder" begins with "w"
-- (true,false,false,true)
An each expression can also include a where clause to select a subset of the items in the list. The word each can be used within the where clause to refer to each source item:
put each word of "Wisdom begins in wonder" \
where each begins with "w" -- ("Wisdom","wonder")
put each item of (1,2,3,4,5,6,7,8,9) where the square root \
of each is an integer -- (1,4,9)
- In an each expression, "item" is assumed if no chunk type is specified. So, for example, the following are equivalent:
put each item of the keys of plist
put each of the keys of plist
- A for each expression extends the functionality of each expressions. More complex expressions can be evaluated that use "each" to refer to each value provided by an each expression, by following that expression with for each as in this example:
put each & "^2 = " & each^2 for each item in (2,3,4,5)
--> ("2^2 = 4","3^2 = 9","4^2 = 16","5^2 = 25")
- Parentheses can be used around "for each" expressions and "where" clauses to enhance readability and limit the scope of the expression:
put each.first && each.last (for each item in employeeList)
put each line of sales (where item 3 of each > 5000)
- It is also possible to chain each expressions when working with nested lists:
set myList to ((1,2,3),(4,5))
put each item of each item of myList & "x" -- ((1x,2x,3x),(4x,5x))
- It is possible to use more complex expressions that combine each expressions, like these:
put each item of "A".."C" & each item of 1..4
-- ((A1,A2,A3,A4),(B1,B2,B3,B4),(C1,C2,C3,C4))
put each item of 1..10 times each item of 1..10 into timesTable
- New in 2.04: each expressions can also apply operations to containers.
- You can use each expressions with many commands to apply the command action across many items in a list or text chunks in a string (lines, words, items, or characters). A where, which, or whose clause can be used to select specific values to be affected. The commands that support this include:
put
,set
,insert
,push
,pull
,pop
,replace
,sort
,split
,join
,add
,subtract
,multiply
,divide
,reverse
,shuffle
,add properties
,replace properties
,remove properties
,retain properties
,rename properties
, andread
.
- You can use each expressions with many commands to apply the command action across many items in a list or text chunks in a string (lines, words, items, or characters). A where, which, or whose clause can be used to select specific values to be affected. The commands that support this include:
set each item of grandTotals to zero — sets each item in a list
add 5 to each item of scores which is less than 20
put "*" after each word of text which ends with "ology"
rename each item of recordList using internalRecordNames
pull each item of readings which is more than 100 into highValues