メインコンテンツまでスキップ

Each 式

Each 式は一連の値を明確かつ簡潔に識別することを可能にします。それらは一度に複数の値を選択し操作するための強力なメカニズムを提供します。単一の each 式は、簡潔で読みやすい方法で大量の作業を処理します。

each 式の使用方法は二つあります:

  • 値のリストを選択または生成するため、
  • 値のセットを変更するため。

さらに、選択された値のセットが全て与えられたテストを通過するかどうかをテストするために使用される密接に関連した式のセット(every 式と呼ばれます)があります。

これら三つのメカニズム、つまり「each value」式、「each container」式、および「every」式は、共通点があります。

  • これら三つの式のタイプは全て、リストアイテムまたはテキストのチャンクに作用します。頻繁にリストで作業するために使用されますが、行、テキストアイテム、単語、文字、またはパターンの一致または発生など、任意のチャンクタイプで作業するためにも使用できます。
  • これら三つの式のタイプは、where 節を使用して作用する値のセットを選択できます。where 節は任意です。それがなければ、式はリスト内の全てのアイテムまたはテキストの全てのチャンクに適用されます。where 節が使用されると、作業する値のサブセットを選択するための明確かつ読みやすい方法を提供します。

値の生成に Each 式を使用する

"each value" 式は常に値のリストを生成します、選択された値が単一であっても全くない場合(空リスト)でもです。

例:

put each item of 1..50 where each is a multiple of 7 -- '[7,14,21,28,35,42,49]'を表示します。

例:

put each item of 1 to 20 where the square root of each is an integer -- '[1,4,9,16]'を表示します。

例:

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]' を表示します。

例:

RunWithNewResults TestScript // TestScriptは実行するスクリプトの名前を含む変数です
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 // 結果の中のすべてのスクリーンショットへのファイルパスを含むリストを作成します
SendMail {To:"test@gmail.com",Subject:title & "---Test results" ,body:TestScript & "Results"& Outcome,attachment:Outcome's Logfile &&& myFiles}

Each についての事実

each 式の結果は常にリストです。その最も単純な使用法を考えると、each 式はソース値からの各文字、単語、行、テキストアイテム、またはリストアイテムにアクセスし、それらの値を含むリストを作成します。

where 節は、一定の条件を満たすアイテムを選択することができます。where 節の中では、each 変数は順番にソースからの各値を参照します。where 節が true と評価される値だけが結果のリストに含まれます。

構文:
each chunk of sourceValue {where conditional}
each chunk of sourceValue { { where conditional } }
each chunk of sourceValue { ( where conditional ) }

任意のチャンクタイプ(リストアイテム、テキストアイテム、単語、行、または文字)とともに each 式を使用します。

conditional 式は通常、特殊変数 each を含む式で、これは結果のリストに含める値を選択するために_sourceValue_の各チャンクに設定されます。where 節は、読みやすさやステートメントの他の部分からの分離のために中括弧(または丸括弧)で囲むことができます(互換性のために、everyとともに中括弧が推奨されます)。

大きな式内の Each 式

each 式が大きな式内に埋め込まれているとき、each 式自体の外側の他の演算子は、each 式によって生成されたリストの値それぞれに適用され、リスト全体に対してではありません。

例:

put the length of each word of "four score and twenty"
-- Each 式はリスト ["four", "score", "and", "twenty"] を生成し、
-- その後、リストの各アイテムに対して length 関数を呼び出します。結果は、
-- 個々の単語の長さのリストです。'[4,5,3,6]' を表示します。

Each 式のスコープを制限する

each 式が影響を及ぼす範囲を広げ、全体の式が結果のリストの各アイテムに適用されるようにするという事実は、その力を大いに増強します。しかし、期待される結果を得るために、この影響を制限することが重要な時もあります。

例:

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
-- Each 式はリスト ["the","the"] を返し、その後、
-- "アイテム数の"演算子がこのリストの各アイテムに適用されます。
-- 結果として 2つの 1 が得られます (単語 "the" は単一のアイテムです)。
-- リスト '[1,1]' を表示します。

each 式の影響範囲を制限するために括弧を使用します。

例:

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
-- リスト '[1,1]' を表示します
put the number of items in (each item of planets where the length of each is greater than 4)
-- "アイテム数の" が each 式の結果全体、つまりリストに適用されるようにします。
-- これにより、リストの単語の数が4以上のものが得られます。
-- '2' を表示します。

For Each 式を使用してスコープを拡張する

括弧が each 式のスコープを制限するという事実は、使用可能な表現の種類を制限します。例えば、括弧を使用して関数を呼び出し、それを各値に適用することはできません。なぜなら、括弧は each 式のスコープを制限するため、関数は結果のリスト全体に対して呼び出されるだけで、各値には呼び出されません。

この問題を克服するために for each 式を使用できます。

例:

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

ここでは、特別なeach変数を使用した表現が与えられ、その後にfor eachから始まるeach表現が続きます。for eachの前に来る表現には特別な制限はありません、そしてeach変数を一度以上使用することができます。この例は、実行可能な操作の種類に完全な柔軟性を提供します。

例:

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

上記のSenseTalkコードは、実行されると以下のように表示されます:

Longest words in text:
ancient has 7 letters
anteater has 8 letters
antiquarian has 11 letters

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

_resultExpression_は、通常も特別なeach変数を使用する表現です。それはeach表現によって生成された各値に対して評価され、全体の表現の最終的な値のリストを生成します。for each節は、読みやすさを強化するためや、他のステートメントの部分から分離を提供するために、括弧で囲むことができます。

_conditional_表現は、通常、特別なeach変数を含む表現で、_sourceValue_の各チャンクに設定され、結果リストに含める値を選択するために使用されます。

ネストしたEach表現

each表現は、別のeach表現で囲むことができます。これは混乱を招くかもしれませんが、ネストしたリストや他のネストした構造体を扱うときに役立つかもしれません。例えば、各単語の長さを取得する(単一のフレーズではなく、一連のフレーズの各々について)ために、次の例のように行うことができます:

例:

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]'

ネストしたeach表現ではwhere節を使用することができます。各whereを最も近いeachに合わせることに注意する必要があります。例えば、3文字以上の単語の単語の長さだけを見たい場合、以下の例を使用することができます:

例:

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.
-- Displays the nested list '[[8],[5,3]]' which contains the lengths of the words only

ここでは、最初の where 句、つまり where true が必要な理由を説明します。これがないと、他の where 句が各行に適用されますが、行の長さではなく、個々の単語の長さが重要です。

結合したEach式

大きな式の一部として2つの each 式を結合して、効果を倍にし、結果のネストしたリストを作成できます。

例:

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..3 times each item of 1..3 into timesTable
-- ネストしたリスト '[[1,2,3],[2,4,6],[3,6,9]]' を変数に格納します。

Each式でのCounter

each 式で counter 関数を使用すると、その関数はソース内の現在のアイテムの数に評価されます。詳細は Repeat Loops を参照してください。

例:

put each char of "abcdefg" & counter()
-- カウンタの値によって、文字列の各文字に番号を関連付けます。
-- with each character of the string.
-- リスト '[a1,b2,c3,d4,e5,f6,g7]' を表示します。

この能力は、each 式の中でエンクロージングループから counter 値が必要な場合、最初に counter 値を変数に代入する必要があることを意味します。

ノート

counter 関数の同義語として repeatIndex を使用できます。

例:

set FarmAnimals to {{
pig
cow
chicken
}}
}}
put each line of FarmAnimals & " is on line " & the counter

Repeat With Each

Repeat with each は、each 式と組み合わせると、文字列を簡単に評価し、各イテレーション中に結果のリストのアイテムに対してすぐに操作を実行するリピートループのタイプです。

例:

repeat with each item of ["dog","cat","horse"] which starts with "c" -- 元のリストを評価して新しいリスト '[cat]' を作成し、新しいリストの各アイテムで反復します
put it & " meows" -- 'cat meows'を表示します
end repeat

例:

repeat with each word of "To infinity and beyond!" whose length is greater than 3
log it -- 最初の反復では "infinity" をログに出力し、2回目の反復では "beyond!" をログに出力します
typetext it
end repeat

値を変更するためのEach式の使用

Each式が値が期待される状況で使用されると("each value"式として知られています)、それは値のリストを生成します。これは Each式を使用して値を生成する で説明されています。Each式はまた、宛先コンテナが期待される状況で使用することもできます("each container"式として知られています)。 "コンテナ"とは、コマンドによって変更される値を意味します。このように使用すると、each式はコマンドによって変更される各値のリストを選択して提供できます。

例えば、addコマンドを考えてみましょう。これは add aValue to aContainer の形で表現されます。通常、コンテナは変数で、aValue がそれに加えられることによってその値が変更されます。

add 7 to total

each式をコンテナ(この場合は total)の代わりに使用すると、一度に全体の値セットを変更できます:

add 7 to each item of vacationTotals

where句を使用すると、どの値を変更するかを選択できます:

add 7 to each item of vacationTotals which is less than 20

"each container"式が使用できるコマンドには次のものがあります: Set, Put, Add, Subtract, Multiply, Divide, Insert, Push, Pop, Pull, Split, Join, Replace, Sort, Read, Reverse, Shuffle, Add Properties, Remove Properties, Replace Properties, Retain Properties, Rename Properties

例:

set each item of grandTotals to zero -- リストの各アイテムを設定します
set numbers to [4,1,26,33,8,72,5,12]
multiply each item of numbers {which is a multiple of 3} by 100
put numbers --> [4,1,26,3300,8,7200,5,1200]
set numbers to [408,22,7,123,45,9,265,38]
pull each item of numbers which is greater than 100 into higherNumbers
put numbers --> [22,7,45,9,38]
put higherNumbers --> [408,123,265]

例:

set sentence to "The rain in Spain falls mainly in the plain"
set each word of sentence {where each contains "ain"} to "[" & each & "]"
put sentence —> "The [rain] in [Spain] falls [mainly] in the [plain]"

例:

rename each item of recordList using internalRecordNames

値をテストするためのEvery式の使用

every式は each式に似ています。each式が値のリストを返すのに対して、every式はTrueまたはFalseの単一の値を返し、すべての評価された式が真であるかどうかを示します。

every式は常にTrueまたはFalseとして評価されるため、通常はif文の条件として使用されます。たとえば、数字のリストのすべての値が0より大きいかどうかをテストするには、次のように言うことができます:

if every item of numList is greater than 0 then ...

every式はそれらが返す値においてeach式と異なりますが、どちらも where句(または whichwhose)を使用できる点で同じです。ただし、every式ではこれらは { } で囲まれる 必要があります 。また、どちらのタイプの式も、括弧で囲まれていない限り、式の周囲の演算子を含むように"拡大"します。

例:

リスト grades の中の各プロパティリストのスコアプロパティをチェックします。

the score of every item of grades is at least 90

every式には4つの異なるバリエーションがあります(実際には2つだけが"every"という言葉を使用していますが、その振る舞いと使用法が非常に似ているため、すべてをまとめています):

  • every: 選択されたエントリすべてが真である場合に限り、真を返します;
  • not every: 少なくとも1つの選択されたエントリが真でない場合、真を返します;
  • at least one: 少なくとも1つの選択されたエントリが真である場合に真を返し(そして全てが真でない場合は偽を返します);
  • none ofまたはnot one of: 選択されたエントリが真でない場合に真を返します。
ノート

これらの式は、式の真偽が判断できるようになるとすぐに値の評価を停止します(例えば、everyによって評価された最初の選択エントリが偽である場合、全体の結果が偽になることがわかるため、他のエントリは評価されません)。

構文:
すべての {シングル} chunkType [ | ] sourceValue { { [where conditionWithEach | which operator value | whose propertyCondition] } } operator value

すべてのない {シングル} chunkType [ | ] sourceValue { { [where conditionWithEach | which operator value | whose propertyCondition] } } operator value

少なくとも一つの { {その}} chunkType [ | ] sourceValue { { [where conditionWithEach | which operator value | whose propertyCondition] } } operator value

[一つもない { {その}} | どれもない {その}] chunkType [ | ] sourceValue { { [where conditionWithEach | which operator value | whose propertyCondition] } } operator value

例:

set numList to [1,3,7,12,43,99]
put every item of numList is a number —> True
put not every item of numList is divisible by 3 —> True
put at least one item in numList is divisible by 3 —> True
put at least one item in numList is 77 —> False
put not one item in numList is less than 0 —> True
put none of the items in numList is equal to 500 —> True

Where句

where節(またはwhichwhose)は、ある条件を満たすアイテムを選択するのに使用します。where節内では、each変数はソースからの各値を順番に参照します。where節がtrueと評価される値のみが結果のリストに含まれます。

Whichwhoseは、それぞれeachまたはeach'sの代わりに、より複雑なwhere節内でも使用できます。したがって、これら2つの表現は等価です。

each item of numList where each is more than 99
each item of numList which is more than 99

whichwhere eachの代わりに使用できます。従って、これら2つの表現は等価です。

each item of cards where each's suit is "clubs"
each item of cards whose suit is "clubs"

同様に、whosewhere each'sの代わりに使用できます。従って、これら2つの表現は等価です。

each item of nums where each is more than 99 and each is an even number
each item of nums which is more than 99 and which is an even number

明確性を高めるために、where節は中括弧(カーリーブレイス)で囲むことができます(each表現にも括弧を使用することができますが、推奨されません)。 every表現に曖昧性を避けるために(これらは常に他の値との比較で終わります)、every表現で使用されるwhere節は中括弧で囲む必要があります。 このため、一貫性と可読性を高めるために、すべてのwhere節を中括弧で囲む習慣を採用することをお勧めします。