Each Expressions

An each expression is a powerful mechanism for collecting or generating information for each value or a selected subset of the values provided by a list, range, or custom iterator. A single each expression processes a lot of work, in a simple and readable way. It is possible to use each expressions to iterate over words, list and text items, property list keys, characters, lines, and more. When combined with repeat loops, each expressions can be particularly powerful.

Example:

put each item of 1..50 where each is a multiple of 7 // Displays '(7,14,21,28,35,42,49)'

Example:

put each item of 1 to 20 where the square root of each is an integer // Displays '(1,4,9,16)'

Example:

put "Mary Mary quite contrary how does your garden grow" into rhyme

put each word of rhyme // Displays '(Mary,Mary,quite,contrary,how,does,your,garden,grow)'

put each word of rhyme where the length of each is 4 // Displays '(Mary,Mary,does,your,grow)'

put each word of rhyme where each ends with "ary" // Displays '(Mary,Mary,contrary)'

put the length of each word of rhyme where each ends with "ary" // Displays '(4,4,8)'

put each word of rhyme whose length is 4 and which contains "a" // Displays '(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 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.

Use a which or whose clause to select items that meet some condition. An expression can contain both which and whose.

Use an each expression with any chunk type (list items, text items, words, lines, or characters).

Syntax:

each chunk of sourceValue {where conditional}

each chunk of sourceValue {(where conditional)}

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 parentheses for readability or separation from other parts of a statement.

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 eachexpression'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 words in the list with 4 or more letters. Displays '2'.

Expanding Scope with For Each Expressions

The fact that parentheses limit the scope of an each expression restricts the type of usable expressions. For example, you cannot call a function using parentheses and have it apply to each value because the parentheses limit the scope of the each expression. So the function is called for the resulting list as a whole rather than for each value.

A for each expression can be used to overcome this problem.

Example:

put round(each,1) for each item of 2.2 to 3.1 by .25 // Displays '(2.2,2.5,2.7,3)'

Here, an expression is given that uses the special each variable, followed by an each expression beginning with the words for each. The expression that comes before for each has no special restrictions, and can use the each variable more than once. This example gives complete flexibility in the kinds of operations that can be performed.

Example:

set text to "an ancient anteater sat with my antiquarian aunt"

put "Longest words in text:"

get each & " has " & length(each) & " letters" for each word of text where length(each) > 4

put it joined by return

The above SenseTalk code displays as follows when executed:

Longest words in text:

ancient has 7 letters

anteater has 8 letters

antiquarian has 11 letters

Syntax:

resultExpression for each chunk of sourceValue {where conditional}

resultExpression (for each chunk of sourceValue {where conditional})

The conditional expression is usually an expression involving the special each variable , which is set to each chunk of sourceValue in order to select which values to include in the resulting list.

resultExpression is usually also an expression using the special each variable. It is evaluated for each of the values produced by the each expression to produce the final list of values for the overall expression. The for each clause can be enclosed in parentheses to enhance readability or provide separation from other parts of a statement.

Nested Each Expressions

An each expression can be enclosed in another each expression. Although this might become confusing, it might be useful when working with nested lists or other nested structures. For example, to obtain the lengths of each word (not for a single phrase, but for each of a series of phrases) you could do it as shown in the following example:

Example:

set phrases to {{

universal truth

magic is in the eye of the beholder

all is fair in love and war

}}

put the length of each word of phrases // Displays a single list: '(9,5,5,2,2,3,3,2,3,8,3,2,4,2,4,3,3)'

put the length of each word of each line of phrases // Displays a nested list, one list for each line of phrases: '((9,5),(5,2,2,3,3,2,3,8),(3,2,4,2,4,3,3))'

put the number of words of each line of phrases // Displays the list '(2,8,7)'

You can use where clauses in nested each expressions. You must be careful to match each where to the nearest each. For example, if you only wanted to see the word lengths for words that are longer than three characters, you might use the following example:

Example:

set myData to {{

elephant

jelly jar

tea

}}

put the length of each word of each line of myData where true where length of each > 3 // Uses "where true" to indicate interest in the lengths of the individual words rather than the lengths of the lines. Displays the nested list '((8),(5),())' as only the first and second lines have any words with length greater than 3.

put the length of each word of each line of myData where length of each > 3 // Validates the length of the lines rather than the length of the words within the line

put the length of each word of each line of myData where length of each > 3 // Validates the length of the lines rather than the length of the words within the line. Displays the nested list '((8),(5,3))' which contains the lengths of the words only from the lines that have lengths greater than 3.

Here, the first where clause, where true, is needed because without it, the other where clause applies to each line, but you care about the lengths of the individual words, not the lengths of the lines.

Combined Each Expressions

You may combine two each expressions as a part of a larger expression to multiply the effect, producing a nested list of results.

Example:

put each item of "A".."C" & each item of 1..4 // Displays the nested list '((A1,A2,A3,A4),(B1,B2,B3,B4),(C1,C2,C3,C4))'

Example:

put each item of 1..3 times each item of 1..3 into timesTable // Stores the nested list '((1,2,3),(2,4,6),(3,6,9))' into a variable

RepeatIndex() in Each Expressions

When you use the repeatindex() function with an each expression, the function evaluates to the number of the current item within the source. See Repeat Loops for more information.

Example:

put each char of "abcdefg" & repeatIndex() // Associates a number, according to the value of the repeatindex, with each character of the string. Displays the list '(a1,b2,c3,d4,e5,f6,g7)'.

This ability also means that if you need the repeatIndex() value from an enclosing loop inside an each expression, you need to first assign the repeatIndex() value to a variable.

Note: You can use counter() as a synonym for the repeatIndex() function.

Example:

set FarmAnimals to {{

pig

cow

chicken

}}

put each line of FarmAnimals & " is on line " & the counter // Displays the list '(pig is on line 1,cow is on line 2,chicken is on line 3)'

Repeat With Each

Repeat with each is a type of repeat loop that, when combined with each expressions, easily evaluates a string, then immediately performs operations against the resulting list's items during each iteration.

Example:

repeat with each item of ("dog","cat","horse") which starts with "c" // Evaluates the original list from which it creates new list '(cat)', and then iterates with each item of the new list

put it & " meows" // Displays 'cat meows'

end repeat

Example:

repeat with each word of "To infinity and beyond!" whose length is greater than 3

log it // Logs "infinity" on the first iteration and "beyond!" on the second iteration

typetext it

end repeat

 

This topic was last updated on February 26, 2019, at 10:29:14 AM.

Eggplant icon Eggplant.io | Documentation Home | User Forums | Support | Copyright © 2019 Eggplant