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"