Working with Chunks
Storing into Chunks
In addition to accessing a portion of a value, chunk expressions can also be used to store into a portion of a value, provided the thing being accessed is a container.
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"
You can also store something before or after a chunk:
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"
Storing into Chunk Ranges
When storing into chunk ranges, the entire range will be replaced:
put "The great grey green gooey goblin" into monster
put "ugly" into words 2 to 5 of monster
put monster --> "The ugly goblin"
Storing into Chunks with Patterns
You can use occurrence
and match
with patterns for storing into chunks much like other chunk types, including storing into ranges of matches in the source string.
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]
For information about using patterns, see SenseTalk Pattern Language Basics.
Storing into Nonexistent Chunks
If you store something into a chunk that is beyond the end of the container you are storing into, SenseTalk does its best to accommodate you. The results are different for different types of chunks. For text items beyond the number of items in the container:
put "mercury,venus,mars" into gods
put "saturn" into item 5 of gods
put gods --> "mercury,venus,mars,,saturn"
Here, the word saturn
was put into the fifth text item of a value that previously had only 3 text items. To accommodate the request, two addtional commas were automatically inserted before the word saturn
so that it would become the new fifth item. The actual character inserted matches the current setting of the itemDelimiter
property.
When storing into list items beyond the end of a list, the results are similar:
put [dog, cat, mouse] into pets
put rabbit into item 7 of pets
put pets --> [dog,cat,mouse,,,,rabbit]
For lines, the behavior is very similar to that for text items. But because the lineDelimiter
can be a list of several possible delimiters, any one of which could indicate a new line, it can't be used to provide the inserted delimiter. Instead, a separate global property called the lineFiller
provides the delimiter string (by default, Return) that is inserted as many times as needed to fill the text out to the requested line number.
For word chunks beyond the end of the text, a simple delimiter is not enough. Because a word delimiter can be any amount of whitespace, simply inserting more spaces won't add more words. So the wordFiller
global property provides a placeholder "word" (by default, "?") to insert along with spaces to fill out the text to the desired number of words:
put "one two three" into someWords
put "seven" into word 7 of someWords
put someWords --> "one two three ? ? ? seven"
For character chunks, the characterFiller
global property (by default, ".") provides text to be repeated as needed to fill the text out to the desired character position:
put "abcdefg" into alpha
put "z" into character 26 of alpha
put alpha --> "abcdefg..................z"
When a negative chunk number larger than the number of chunks is used, the result is similar to the above descriptions for all chunk types, but with fillers or delimiters added at the beginning of the value to achieve the expected result:
put "abc" into backfill
put "X" into character -7 of backfill
put backfill --> "X...abc"
Related Global Properties
As described above, SenseTalk includes global properties to provide filler text for those cases when you use chunk expressions to add characters, lines, or words to chunks that expand them beyond their current limits. These three properteis, the characterFiller
, the lineFiller
, and the wordFiller
, are described in detail on Local and Global Properties for Chunk Expressions.
Storing into Multiple Chunks
You can store into multiple chunks at once by supplying a list of chunk numbers:
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"
You can store multiple values at once by supplying a list of values as well as of chunk numbers:
put ["Old","Ugly"] into words [5,2] of monster
put monster --> "The Ugly Grey Green Old Goblin"
Deleting Chunks
Chunks of containers, besides being stored into, can also be deleted. This is done with the delete
command (described in detail in Text and Data Manipulation):
Example:
put [dog, cat, gorilla, mouse] into pets
delete item 3 of pets
put pets --> [dog, cat, mouse]
Example:
put "My large, lumpy lout of a lap dog is lost." into ad
delete words 2 to 7 of ad
put ad --> "My dog is lost."
Counting Chunks
To find out how many of a given chunk type are present in some value, use the number
function:
Example:
get the number of characters in "extraneously" -- 12
Example:
put number of words in "I knew an old woman" --> 5
Example:
if the number of items in list is less than 12 then ...
Number
Function
Behavior: The number
function counts the number of characters, words, lines, text items, list items, keys, values, or bytes in a value. Use this function whenever you need to determine how many of a particular chunk type are present in a value. If the value is empty, the result will always be zero. In addition to the usual text chunks and bytes, when expression is an object or property list chunks can be "keys", "values", or "properties" to count the number of keys and values that are defined in the object.
Syntax:
{the} number of chunks [in | of] expression
Example:
put "I wept because I had no answers, until I met a man who had no questions." into quote
put the number of characters in quote --> 72
put the number of words in quote --> 16
put the number of items in quote --> 2
put the number of lines in quote --> 1
Testing for Presence of a Chunk Value
You can find out whether a particular value is present as one of the chunks of another value using the is among
or is not among
operator.
Is Among
, Is Not Among
Operators
Behavior: The is among
operator tests whether a particular value is present among the characters, words, lines, text items, list items, keys, values, or bytes in a value. This will only return true if the target value is equal to one of the specified chunks. Contrast this with the is in
or contains operators which will only test whether one text string is a substring of another (see the second example). In addition to the usual text chunks, when expression is an object or property list chunks can be "keys" or "values" to test whether targetValue is one of the keys or values of the object.
Syntax:
targetValue is {not} among {the} chunks of sourceValue {considering case | ignoring case}
Example:
put "be" is among the words of "To be or not to be" --> true
Example:
put "be" is among the words of "I believe I am a bee" --> false
Example:
put 7 is among the items of [5,5+1,5+2,5+3] --> true
Example:
put "M" is not among the characters of "Avogadro" --> true