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]