Inform 7 Home Page / Documentation

§21.9. Accessing entries in a list

The length of a list can change as values are added or removed, and can in principle be any number from 0 upwards. A list with 0 entries is empty. We can find the length with:

number of entries in/of/from (list of values) ... number

This phrase produces the number of positions in the list. Example:

the number of entries in {1, 1, 1, 3, 1}

is 5, even though there are only two genuinely different items in the list.

If the length is N then the entries are numbered from 1 (the front) to N (the back). These entries can be accessed directly by their numbers. For instance,

entry 2 of L

refers to the second entry of L: it can be used as a value, or changed, just as if it were a named variable. For instance, we could write:

now entry 7 of L is "Spain";
say "The rain in [entry 7 of L] stays mainly in the plain.";

which would (untruthfully) print "The rain in Spain stays mainly in the plain", but only if L had an entry 7 to make use of: if L were a list of 5 entries, say, then a run-time problem results. (And if L cannot hold text, a problem message means that we never get as far as run-time.) Because entries number from 1, this is always incorrect:

entry 0 of L

and if L is currently empty, then there is no entry which can be accessed, so that any use of "entry ... of L" would produce a run-time problem. There are programming languages in the world where accessing entry 100 in a 7-entry list automatically extends it to be 100 entries long: Inform is not one of them. But see the next section for how to change list lengths explicitly.

arrow-up.png Start of Chapter 21: Lists
arrow-left.png Back to §21.8. Sorting, reversing and rotating lists
arrow-right.png Onward to §21.10. Lengthening or shortening a list

***ExampleRobo 2
A robot which watches and records the player's actions, then tries to repeat them back in the same order when he is switched into play-back mode.