§21.10. Lengthening or shortening a list
We can explicitly change the length of a list like so:
change (list of values) to have (number) entries/entry
This phrase alters the given list so that it now has exactly the number of entries given. Example:
change L to have 21 entries;
If L previously had more than 21 entries, they are thrown away (and lost forever); if L previously had fewer, then new entries are created, using the default value for whatever kind of value L holds. So extending a list of numbers will pad it out with 0s, but extending a list of texts will pad it out with the empty text "", and so on.
We can also write the equivalent phrases:
truncate (list of values) to (number) entries/entry
This phrase alters the given list so that it now has no more than the number of entries given. Example:
truncate L to 8 entries;
shortens L to length 8 if it is currently longer than that, trimming entries from the end, but would (for instance) leave a list of length 3 unchanged. Note that
truncate L to 0 entries;
empties it to { }, the list with nothing in.
truncate (list of values) to the first (number) entries/entry
This phrase alters the given list so that it now consists only of the initial part of the list with the given length. Example:
truncate L to the first 4 entries;
turns {1, 3, 5, 7, 9, 11} to {1, 3, 5, 7}.
truncate (list of values) to the last (number) entries/entry
This phrase alters the given list so that it now consists only of the final part of the list with the given length. Example:
truncate L to the last 4 entries;
turns {1, 3, 5, 7, 9, 11} to {5, 7, 9, 11}.
But we don't have to truncate: we can also -
extend (list of values) to (number) entries/entry
This phrase pads out the list with default values as needed so that it now has at least the given length. (If the list is already at least that length, nothing is done.) Example:
extend L to 80 entries;
lengthens L to length 80 if it is currently shorter than that.
For example,
To check sorting (N - a number):
let L be a list of numbers;
extend L to N entries;
repeat with X running from 1 to N:
now entry X of L is X;
say "L unrandomised is [L].";
sort L in random order;
say "L randomised is [L].";
sort L;
say "L in ascending order is [L]."
builds a list of N numbers (initially all 0), fills it with the numbers 1, 2, 3, ..., N, then randomly reorders them, then sorts them back again, recovering the original order. The text produced by "check sorting 10" depends partly on chance but might for instance be:
L unrandomised is 1, 2, 3, 4, 5, 6, 7, 8, 9 and 10.
L randomised is 6, 2, 9, 3, 10, 1, 7, 4, 8 and 5.
L in ascending order is 1, 2, 3, 4, 5, 6, 7, 8, 9 and 10.
As with text in the previous chapter, a project which needs really long lists should use the Glulx virtual machine - "check sorting 10000", for instance, would break the default memory environment on the Z-machine, which is very tight, but works fine (if not very rapidly) on Glulx.
![]() | Start of Chapter 21: Lists |
![]() | Back to §21.9. Accessing entries in a list |
![]() | Onward to §21.11. Variations: arrays, logs, queues, stacks, sets, sieves and rings |
Suppose (as in Infocom's Leather Goddesses of Phobos) that we have a maze that the player can escape only by performing the correct sequence of actions in the correct order. One way to do this would be to keep a list of the player's most recent actions, and see whether these match up with the combination we have established as the maze's solution. For instance:
|
|
Suppose (as in Infocom's Leather Goddesses of Phobos) that we have a maze that the player can escape only by performing the correct sequence of actions in the correct order. One way to do this would be to keep a list of the player's most recent actions, and see whether these match up with the combination we have established as the maze's solution. For instance:
Suppose (as in Infocom's Leather Goddesses of Phobos) that we have a maze that the player can escape only by performing the correct sequence of actions in the correct order. One way to do this would be to keep a list of the player's most recent actions, and see whether these match up with the combination we have established as the maze's solution. For instance:
|
|