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

チャンクを利用する

チャンクに保存する

値の一部にアクセスするだけでなく、チャンク表現は、アクセスされているものがコンテナである場合、値の一部に保存するためにも使用できます。

put "Jack Peterson" into name
put "d" into char 3 of last word of name
put "e" into char -2 of name
put "Olaf" into first word of name
put name -- "Olaf Pedersen"

また、チャンクの前後に何かを保存することもできます。

put "The plant is growing" into phrase
put "egg" before word 2 of phrase
put " purple" after word 1 of phrase
put phrase -- "The purple eggplant is growing"

チャンクの範囲に保存する

チャンクの範囲に保存すると、その範囲全体が置き換えられます。

put "The great grey green gooey goblin" into monster
put "ugly" into words 2 to 5 of monster
put monster-- "The ugly goblin"

パターンを使用したチャンクに保存する

他のチャンクタイプと同様に、occurrencematchをパターンとともに使用してチャンクに保存することができます。これには、ソース文字列内のマッチの範囲に保存することも含まれます。

set text to "[a]hello[b]bonjour[c]hola[d]"
set marker to <"[", character, "]">
put occurrences 2 to 3 of marker in text --> ([b],[c])
put "$$$" into occurrences 2 to 3 of marker in text
put text --> [a]hello$$$hola[d]

パターンの使用については、SenseTalkパターン言語基礎を参照してください。

存在しないチャンクに保存する

保存しているコンテナの終わりを超えたチャンクに何かを保存すると、SenseTalkは最善を尽くして対応します。結果はチャンクのタイプによって異なります。コンテナ内のアイテム数を超えたテキストアイテムの場合:

put "mercury,venus,mars" into gods
put "saturn" into item 5 of gods
put gods-- "mercury,venus,mars,,saturn"

ここでは、saturnという単語が、以前は3つのテキストアイテムしかなかったの値の5番目のテキストアイテムに入れられました。要求に対応するために、単語saturnの前に2つの追加のカンマが自動的に挿入され、それが新しい5番目のアイテムとなりました。挿入される実際の文字はthe itemDelimiterプロパティの現在の設定に一致します。

リストの終わりを超えたリストアイテムに保存すると、結果は同様です:

put [dog, cat, mouse] into pets
put rabbit into item 7 of pets
put pets-- [dog,cat,mouse,,,,rabbit]

行については、テキストアイテムと非常に類似した動作をします。しかし、the lineDelimiterは新しい行を示す可能性のあるいくつかの可能なデリミタのリストであるため、挿入するデリミタを提供するために使用することはできません。代わりに、the lineFillerという別のグローバルプロパティが、必要なだけ挿入されるデリミタ文字列(デフォルトではReturn)を提供します。

テキストの終わりを超えた単語チャンクについては、単純なデリミタだけでは十分ではありません。単語デリミタは任意の量の空白であるため、単にスペースを挿入するだけでは単語が増えません。そこで、the wordFillerグローバルプロパティが、テキストを必要な単語数まで埋めるためにスペースと一緒に挿入するプレースホルダー"単語"(デフォルトでは"?")を提供します:

put "one two three" into someWords
put "seven" into word 7 of someWords
put someWords-- "one two three ? ? ? seven"

文字のチャンクについては、the characterFillerグローバルプロパティ(デフォルトでは".")が、テキストを必要なだけ繰り返して、テキストを望ましい文字位置まで埋めるためのテキストを提供します:

put "abcdefg" into alpha
put "z" into character 26 of alpha
put alpha-- "abcdefg..................z"

チャンクの数よりも大きな_負の_チャンク数が使用されると、結果は上記のすべてのチャンクタイプに対する説明と似ていますが、期待される結果を得るために、値の_開始_部分にフィラーやデリミタが追加されます:

put "abc" into backfill
put "X" into character -7 of backfill
put backfill-- "X...abc"

上記の説明のように、SenseTalkはグローバルプロパティを含み、現在の制限を超えて拡大するチャンクに文字、行、単語を追加するためのチャンク表現を使用する場合に、フィラーテキストを提供します。これら三つのプロパティ、the characterFillerthe lineFiller、およびthe wordFillerは、Local and Global Properties for Chunk Expressionsで詳しく説明されています。

複数のチャンクに保存する

一度に複数のチャンクに保存するためには、チャンク番号のリストを提供します:

put "The great grey green gooey goblin" into monster
put "G" into chars [5,11,16,22,28] of monster
put monster -- "The Great Grey Green Gooey Goblin"

チャンク番号だけでなく値のリストも提供することで、一度に複数の値を保存することができます:

put ["Old","Ugly"] into words [5,2] of monster
put monster -- "The Ugly Grey Green Old Goblin"

チャンクの削除

コンテナのチャンクは保存するだけでなく削除することもできます。これはdeleteコマンド(詳しくはText and Data Manipulationを参照)で行います:

例:

put [dog, cat, gorilla, mouse] into pets
delete item 3 of pets-- [dog, cat, mouse]
put pets --> [dog, cat, mouse]

例:

put "My large, lumpy lout of a lap dog is lost." into ad
delete words 2 to 7 of ad-- "My dog is lost."
put ad --> "My dog is lost."

Chunksの数え方

ある値に特定のチャンクタイプがいくつ存在するかを知るためには、number関数を使用します:

例:

get the number of characters in "extraneously"-- 12

例:

put number of words in "I knew an old woman"-- 5

例:

if the number of items in list is less than 12 then ...

Number関数

振る舞い: number関数は、値内の文字、単語、行、テキストアイテム、リストアイテム、キー、値、またはバイトの数を数えます。特定のチャンクタイプが値内にどれだけ存在するかを判断する必要があるときはいつでもこの関数を使用します。値が空である場合、結果は常にゼロになります。通常のテキストチャンクとバイトに加えて、_expression_がオブジェクトやプロパティリストの場合、_chunks_はオブジェクトに定義されているキーまたは値の数を数えるための"keys"または"values"にすることができます。

文法:
{the} number of chunks [in | of] expression

例:

put "I wept because I had no answers, until I met a man who had no questions." into quote
quoteの文字数を取得します -- 72
quoteの単語数を取得します -- 16
quoteのアイテム数を取得します -- 2
quoteの行数を取得します -- 1

Chunk値の有無をテストする

特定の値が別の値のチャンクの一部として存在するかどうかを、is amongまたはis not among演算子を使用して調べることができます。

Is Among, Is Not Among演算子

振る舞い: is among演算子は、特定の値が文字、単語、行、テキスト項目、リスト項目、キー、値、またはバイトのいずれかに存在するかどうかをテストします。これは、ターゲット値が指定されたチャンクのいずれかと等しい場合にのみ真を返します。これと対照的に、is inまたはcontains演算子は、一つのテキスト文字列が他の文字列の部分文字列であるかどうかをテストします(第二の例を参照)。通常のテキストチャンクに加えて、_expression_がオブジェクトまたはプロパティリストの場合、_chunks_を"keys"または"values"にすることで、_targetValue_がオブジェクトのキーまたは値のいずれかであるかどうかをテストします。

文法:
targetValue is {not} among {the} chunks of sourceValue {considering case | ignoring case}

例:

"To be or not to be"の単語の中に"be"があるかどうか -- 真

例:

"I believe I am a bee"の単語の中に"be"があるかどうか -- 偽

例:

[5,5+1,5+2,5+3]のアイテムの中に7があるかどうか -- 真

例:

"Avogadro"の文字の中に"M"がないかどうか -- 真

値のチャンク位置の決定

値内の文字、単語、行、テキスト項目、リスト項目の序数位置を調べることができます(検索はケースに依存せず、“considering case”または“with case”が指定されていない限り)。ターゲットの式が見つからない場合は0が返されます:

文法:> {the} chunk number of targetValue within sourceValue {considering case | ignoring case}
{the} chunk number of targetValue within sourceValue {considering case | ignoring case}
{the} chunk number containing targetValue within sourceValue {considering case | ignoring case}

例:

"The rain, in Spain, is mainly in the plain"をテキストに入れます
テキスト内の"t"の文字数を取得します -- 1
大文字小文字を考慮したテキスト内の"t"の文字数を取得します -- 34
テキスト内の" in Spain"のテキストアイテム数を取得します -- 2
テキスト内の"mainly"の単語数を取得します -- 6
テキスト内の"another line"の行数を取得します -- 0

値を含む単語、行、またはアイテム番号を見つけるには(値と等しいものではなく)、ofの代わりにcontainingを使用します:

テキスト内の"main"の単語数を取得します -- 0
テキスト内に"main"を含む単語数を取得します -- 6
テキスト内に"Spain"を含むテキストアイテム数を取得します -- 2

チャンク値の出現回数のカウント

ソース値内で特定のチャンク値が何回発生するかをカウントするには、number of occurrencesまたはnumber of instances関数を使用します。

文法:
{the} number of {occurrences of} targetValue among {the} chunks of sourceValue {considering case | ignoring case}

例:

"banana"の文字の中にある"a"の出現数を取得します -- 3

例:

put the number of instances of "be" among the words of "to be or not to be"-- 2

例:

put the number of occurrences of 15 among the items delimited by "-" of "315-15-4152"-- 1

例:

put the number of occurrences of <digit> in "64W x 8H" // 3

特定のチャンクタイプが指定されていない場合、ソース値がリストまたはオブジェクトでない限り文字が想定されます。ソース値がリストまたはオブジェクトの場合、それぞれリストアイテムまたはプロパティが想定されます:

put number of occurrences of "a" in "banana" -- 3

例:

put the number of instances of 3 in [1,3,5,6,3,2] -- 2

例:

put number of occurrences of "Do" in "Do,re,mi,do" -- 2

大文字と小文字を区別する比較を行うには、"considering case"を使用します(またはcaseSensitiveプロパティをtrueに設定します)。

例:

put number of instances of "Do" in "Do,re,mi,do" considering case-- 1

特殊なケースとして、"among the characters of"は、単一の文字の出現回数だけでなく、文字列の出現回数をカウントするためにも使用できます。

例:

put number of instances of "na" among the chars of "banana" -- 2

値のすべてのチャンクを反復処理する

値の各チャンクで何かをするには、repeatコマンドのrepeat with each形式を使用します(これはスクリプトの構造と制御フローでも説明されています)。

例:

repeat with each line in file "/tmp/output"
if the first word of it is "Error:" then put it
end repeat

Each式を使用してチャンクのリストを抽出する

形式 each chunkType of sourceValue の任意の表現は、そのタイプのすべてのチャンクを含むリストを生成します。さらに、特定の基準に一致するチャンクを選択するためにwhere句を使用することもできます。

文法:
each chunk of sourceExpr {where conditional}

例:

put each character of "Sweet!" -- ["S","w","e","e","t","!"]

例:

put each word of "Wisdom begins in wonder" -- ["Wisdom","begins","in","wonder"]

より興味深いことに、each表現はより大きな表現の一部になることができます。大きな表現の中で、演算子はリスト全体ではなくリストの各項目に適用されます。

例:

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]

括弧は、大きなeach表現のスコープを制限し、リスト全体に適用する動作に制限します。

例:

put sum of the length of each word in "Wisdom begins in wonder" -- [6,6,2,6]
put sum of (the length of each word in "Wisdom begins in wonder") -- 20

each表現には、リストの項目の一部を選択するためのwhere句も含めることができます。where句内でeachを使用すると、各ソース項目を参照することができます。

例:

"「Wisdom begins in wonder」の各単語を出力する。それぞれが「w」で始まる場合 -- [「Wisdom」、「wonder」]"

例:

"「1、2、3、4、5、6、7、8、9」の各アイテムを出力する。各アイテムの平方根が整数である場合 -- [1,4,9]"

関連項目: