19.3. Characters, words, punctuated words, unpunctuated words, lines, paragraphs

| Chapter 19: Advanced Text |

| 19.3. Characters, words, punctuated words, unpunctuated words, lines, paragraphs

Suppose we have some text called, once again, WHATEVER. Inform can get at its contents in a variety of ways. The lowest-level is by character - a character is a letter, digit, punctuation symbol, space or other letter-form. (We use the term "character" rather than "letter" because otherwise we would have to call "5" a letter, and so on.) Characters number upwards from 1: character number 1, to repeat that, starts the text. We can get the Nth character with:

character number (number) in (indexed text) ... indexed text

This phrase produces the Nth character from the text, counting from 1. Characters include letters, digits, punctuation symbols, spaces or other letter-forms. Example: character number 8 in "numberless projects of social reform" produces "e". If the index is less than 1 or more than the length of the text, the result is an empty text, "".

The maximum character number varies with the current length of the text, and can be evaluated as:

number of characters in (indexed text) ... number

This phrase produces the number of characters from the text. Characters include letters, digits, punctuation symbols, spaces or other letter-forms. Examples: number of characters in "War and Peace" number of characters in "" produce 13 and 0 respectively.

When an indexed text has 0 characters, it is "empty", just as for ordinary text; and otherwise it is "non-empty".

We can also extract the contents by word, again numbered from 1. Thus:

word number (number) in (indexed text) ... indexed text

This phrase produces the Nth word from the text, counting from 1. Words for this purpose are what's left after breaking the text up at punctuation or spacing (spaces, line breaks, paragraph breaks) and then removing that punctuation or spacing. Example: word number 3 in "ice-hot, don't you think?" produces "don't". If the index is less than 1 or more than the number of words in the text, the result is an empty text, "".

number of words in (indexed text) ... number

This phrase produces the number of words from the text. Words for this purpose are what's left after breaking the text up at punctuation or spacing (spaces, line breaks, paragraph breaks) and then removing that punctuation or spacing. Example: number of words in "ice-hot, don't you think?" produces 5.

Note that the contraction apostrophe in "don't" doesn't count as punctuation. Because this is not always quite what we want, Inform offers two variations:

punctuated word number (number) in (indexed text) ... indexed text

This phrase produces the Nth word from the text, counting from 1. Words for this purpose are what's left after breaking the text up at punctuation or spacing (spaces, line breaks, paragraph breaks) and then removing the spacing, but leaving the punctuation as independent words. Example: punctuated word number 2 in "ice-hot, don't you think?" produces "-". The punctuated words here are "ice", "-", "hot", ",", "don't", "you", "think", "?". If two or more punctuation marks are adjacent, they are counted as different words, except for runs of dashes or periods: thus ",," has two punctuated words, but "--" and "..." have only one each. If the index is less than 1 or more than the number of punctuated words in the text, the result is an empty text, "".

number of punctuated words in (indexed text) ... number

This phrase produces the number of words from the text. Words for this purpose are what's left after breaking the text up at punctuation or spacing (spaces, line breaks, paragraph breaks) and then removing the spacing, but leaving the punctuation as independent words. Example: number of punctuated words in "ice-hot, don't you think?" produces 8; see if you can find them all.

unpunctuated word number (number) in (indexed text) ... indexed text

This phrase produces the Nth word from the text, counting from 1. Words for this purpose are what's left after breaking the text up at spacing (spaces, line breaks, paragraph breaks) but including all punctuation as if it were part of the spelling of the words it joins to. Example: unpunctuated word number 1 in "ice-hot, don't you think?" produces "ice-hot,". The unpunctuated words in "ice-hot, don't you think?" are "ice-hot,", "don't", "you", "think?". If the index is less than 1 or more than the number of punctuated words in the text, the result is an empty text, "".

number of unpunctuated words in (indexed text) ... number

This phrase produces the number of words from the text. Words for this purpose are what's left after breaking the text up at spacing (spaces, line breaks, paragraph breaks) but including all punctuation as if it were part of the spelling of the words it joins to. Example: number of unpunctuated words in "ice-hot, don't you think?" produces just 4.

Finally, on the larger scale still, we also have:

line number (number) in (indexed text) ... indexed text

This phrase produces the Nth line from the text, counting from 1. Unless explicit use is made of line-breaking, lines and paragraphs will be the same - it doesn't refer to lines as visible on screen, because we have no way of knowing what size screen the player might have.

number of lines in (indexed text) ... number

This phrase produces the number of lines in the text. Unless explicit use is made of line-breaking, lines and paragraphs will be the same - it doesn't refer to lines as visible on screen, because we have no way of knowing what size screen the player might have. Example: the number of lines in "Sensational news just in![paragraph break]The Martians have invaded Miranda.[line break](One of the moons of Uranus, that is.)" is 3.

paragraph number (number) in (indexed text) ... indexed text

This phrase produces the Nth paragraph from the text, counting from 1.

number of paragraphs in (indexed text) ... number

This phrase produces the number of paragraphs in the text. Example: the number of paragraphs in "Sensational news just in![paragraph break]The Martians have invaded Miranda.[line break](One of the moons of Uranus, that is.)" is 2.

(Attempting to make large enough texts to have a serious paragraph count is slightly risky if there is not much memory to play with, as on the Z-machine. But the facilities do exist.)

Previous | Contents | Next