Chunk Syntax
On this page:
Single Chunks
Syntax:
Syntax definitions for language elements follow these formatting guidelines:
- boldface: Indicates words and characters that must be typed exactly
- italic: Indicates expressions or other variable elements
- {} (curly braces): Indicate optional elements.
- [] (square brackets) separated by | (vertical pipes): Indicate alternative options where one or the other can be used, but not both.
Example syntax:
In this example, "open file" is required and must be typed exactly. "fileName" is a variable element; it is the path to and name of the file being opened. The following expression is optional and indicates why the file is being opened. If this expression is added, "for" is required and must be typed exactly. One of the following must be included, but only one, and they also must be typed exactly: "reading", "writing", "readwrite", "appending", or "updating".
Chunk expressions for all types of chunks can be expressed in several different ways. You can describe a single chunk element:
put item 17 of scores into quiz3
put word 4 of "Mary had a little lamb" -- "little"
Negative numbers can be used to count backwards from the end of the source, with -1 indicating the last chunk element, -2 for the next-to-last, and so forth:
get item -2 of "apple,banana,pear,orange" -- "pear"
Note: In this and the following syntax descriptions, chunk is used to represent any of the chunk types: character (or its abbreviation, char), word, line, item, text item, or list item. Similarly, chunks represents the plural version of these terms. number is a factor which evaluates to a positive or negative number, and expression is the source or destination value which the chunk refers to. Wherever the word "of" is shown, you may use either “of” or “in”, whichever seems natural to you at the time.
Ordinal Chunks
Syntax:
Syntax definitions for language elements follow these formatting guidelines:
- boldface: Indicates words and characters that must be typed exactly
- italic: Indicates expressions or other variable elements
- {} (curly braces): Indicate optional elements.
- [] (square brackets) separated by | (vertical pipes): Indicate alternative options where one or the other can be used, but not both.
Example syntax:
In this example, "open file" is required and must be typed exactly. "fileName" is a variable element; it is the path to and name of the file being opened. The following expression is optional and indicates why the file is being opened. If this expression is added, "for" is required and must be typed exactly. One of the following must be included, but only one, and they also must be typed exactly: "reading", "writing", "readwrite", "appending", or "updating".
Chunk elements can also be referred to by their ordinal number (first, second, ... , millionth):
get the third item of [9,12,13,42] -- 13
Last, Penultimate, Middle, Any Special Ordinals
In addition to numeric ordinals, there are four “special ordinals” that can be used -- last, penultimate, middle (or mid), and any.
Last will identify the last chunk element (this is the same as specifying -1).
Penultimate identifies the next-to-last chunk element (equivalent to specifying -2).
Middle will select the element closest to the middle, based on the total number of chunk elements of that type.
Any selects an element at random from among all those of the indicated type.
Example:
put any item of "cow,ant,moose,goat,donkey,elephant"
Example:
get the middle character of "serendipity" -- "d"
Example:
put the penultimate word of "Peace is its own reward." -- "own"
Example:
put the last line of gettysburgAddress into finalParagraph
Chunk Ranges
Syntax:
chunks firstNumberOrOrdinal to lastNumberOrOrdinal of expression
chunks range of expression
{the} first number chunks of expression
{the} last number chunks of expression
Syntax definitions for language elements follow these formatting guidelines:
- boldface: Indicates words and characters that must be typed exactly
- italic: Indicates expressions or other variable elements
- {} (curly braces): Indicate optional elements.
- [] (square brackets) separated by | (vertical pipes): Indicate alternative options where one or the other can be used, but not both.
Example syntax:
In this example, "open file" is required and must be typed exactly. "fileName" is a variable element; it is the path to and name of the file being opened. The following expression is optional and indicates why the file is being opened. If this expression is added, "for" is required and must be typed exactly. One of the following must be included, but only one, and they also must be typed exactly: "reading", "writing", "readwrite", "appending", or "updating".
A chunk expression can refer to a range of chunk elements. The chunk range will include the beginning and ending elements specified, along with everything in between.
Example:
put characters 3 to 5 of "dragon" -- "ago"
Example:
put words 1 to 2 of "Alas, poor Yorick!" -- "Alas, poor"
Example:
put items middle to last of [1,2,3,4,5,6,7] -- [4,5,6,7]
Example:
put the second to penultimate items of [1,2,3,4,5,6]-- [2,3,4,5]
Example:
put items -3 to -1 of [1,2,3,4,5,6] -- [4,5,6]
A range value can also be used to access a range of chunks.
Example:
put chars 4..6 of "incredible" -- "red"
Example:
set fruitRange to 7..10
Example:
put chars fruitRange of "now appearing" -- "pear"
For ranges at the beginning or end of a value, you can also specify the “first n” or “last n” chunk elements
Example:
put the last three items of [1,2,3,4,5,6] -- [4,5,6]
Example:
get the first 4 characters of "abracadabra" -- "abra"
Multiple Chunks (Lists of Chunks)
Syntax:
Syntax definitions for language elements follow these formatting guidelines:
- boldface: Indicates words and characters that must be typed exactly
- italic: Indicates expressions or other variable elements
- {} (curly braces): Indicate optional elements.
- [] (square brackets) separated by | (vertical pipes): Indicate alternative options where one or the other can be used, but not both.
Example syntax:
In this example, "open file" is required and must be typed exactly. "fileName" is a variable element; it is the path to and name of the file being opened. The following expression is optional and indicates why the file is being opened. If this expression is added, "for" is required and must be typed exactly. One of the following must be included, but only one, and they also must be typed exactly: "reading", "writing", "readwrite", "appending", or "updating".
You can access several distinct chunks of a source value as a list, by specifying a list of the ordinal chunk numbers.
Example:
put items [1,3,5,7] of scores into oddResults
Example:
put words [4,1,2,-1] of "Mary had a little lamb" -- [little,Mary,had,lamb]
The result of accessing multiple chunks is always a list. Negative numbers and the special ordinals (middle, any, etc.) can also be used.
Example:
get items [-2,"middle"] of "apple,banana,pear,orange,peach" -- [orange,pear]