Chapter 20: Lists
20.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:

the number of entries of L

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:

change entry 7 of L to "Spain";
[or equivalently, 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.


404
*** Example  Robo 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.

RB


PreviousContentsNext