Each Expressions
Each
expressions let you clearly and concisely identify a set of values. They provide a powerful mechanism for selecting and manipulating multiple values at once. A single each expression processes a lot of work in a simple and readable way.
There are two ways of using each
expressions:
- To select or generate a list of values,
- To modify a set of values.
In addition, there is a closely-related set of expressions (called every
expressions), which are used to test whether a selected set of values all pass a given test.
These three mechanisms – "each value" expressions, "each container" expressions, and "every" expressions – have certain things in common.
- All three types of expressions operate on list items, or on chunks of text. While frequently used to work with lists, they can also be used to work with any chunk type: lines, text items, words, characters, or pattern matches or occurrences.
- All three types of expressions can use a
where
clause to select the set of values that they operate on. The where clause is optional. Without it, the expression applies to every item in a list or every chunk of text. When a where clause is used, it provides a clear and readable way to select a subset of values to operate on.
Using Each Expressions to Produce Values
An "each value" expression always produces a list of values, even when there is only a single value selected or no values at all (an empty list).
Example:
put each item of 1..50 where each is a multiple of 7 --> [7,14,21,28,35,42,49]
Example:
put each item of 1 to 20 where the square root of each is an integer --> [1,4,9,16]
Example:
put "Mary Mary quite contrary how does your garden grow" into rhyme
put each word of rhyme --> [Mary,Mary,quite,contrary,how,does,your,garden,grow]
put each word of rhyme where the length of each is 4 --> [Mary,Mary,does,your,grow]
put each word of rhyme where each ends with "ary" --> [Mary,Mary,contrary]
put the length of each word of rhyme where each ends with "ary" --> [4,4,8]
put each word of rhyme whose length is 4 and which contains "a" --> [Mary,Mary]
Example:
RunWithNewResults TestScript // TestScript is a variable containing the name of the script to run
put the result into Outcome
put the long name of each of the files of the folder of Outcome's logfile where each ends with ".png" into myfiles // Creates a list containing the file paths to all of the screenshots in the results
SendMail {To:"test@gmail.com",Subject:title & "---Test results" ,body:TestScript & "Results"& Outcome,attachment:Outcome's Logfile &&& myFiles}
Facts About Each
The result of an each
expression is always a list. Considering its simplest usage, an each
expression accesses each character, word, line, text item, or list item from a source value and creates a list containing those values.
A where
clause (or its variants, which
or whose
) lets you select items that meet some condition. Within a where
clause, the each
variable refers to each value from the source in turn. Only values for which the where
clause evaluates to true
are included in the resulting list.
Syntax:
each chunk of sourceValue {where conditional}
each chunk of sourceValue { { where conditional } }
each chunk of sourceValue { ( where conditional ) }
Use an each
expression with any chunk type (list items, text items, words, lines, or characters).
The conditional expression is usually an expression involving the special variable each
, which is set to each chunk of sourceValue in order to select which values to include in the resulting list. The where
clause can be enclosed in curly braces (or parentheses) for readability or separation from other parts of a statement (curly braces are recommended, for compatibility with every
expressions).
Each Expression within a Larger Expression
When an each
expression is embedded within a larger expression, other operators outside of the each
expression itself are applied to each of the values in the list generated by the each
expression rather than to the list as a whole.
Example:
put the length of each word of "four score and twenty"
-- The each expression generates the list ["four", "score", "and", "twenty"]
-- and then calls the length function on each item in the list, resulting in
-- a list of the individual word lengths. Displays '[4,5,3,6]'.
Limiting the Scope of an Each Expression
The fact that an each
expression spreads its influence to the enclosing expression, causing the entire expression to apply to each item in the resulting list, adds tremendously to its power. However, there are times when it is important to be able to limit this effect in order to get the desired result.
Example:
set text to "the flowers of the forest"
put the number of items in each word of text where the length of each is 3
-- The each expression returns the list ["the","the"] and then
-- the "number of items in" operator is applied to each item in this list,
-- resulting in two 1s (since the word "the" is a single item).
-- Displays the list '[1,1]'.
Use parentheses to limit the scope of the each
expression's influence.
Example:
put ["Mars","Venus","Saturn"] into planets
put the number of items in each item of planets where the length of each is greater than 4
-- Displays the list '[1,1]'
put the number of items in (each item of planets where the length of each is greater than 4)
-- Causes "the number of items in" to be applied to the result of the each expression
-- as a whole list, giving the number of words in the list with more than 4 letters.
-- Displays '2'.