Inform 7 Home Page / Documentation


§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.


arrow-up.png Start of Chapter 21: Lists
arrow-left.png Back to §21.9. Accessing entries in a list
arrow-right.png Onward to §21.11. Variations: arrays, logs, queues, stacks, sets, sieves and rings

*ExampleLeopard-skin
A maze that the player can escape if he performs an exact sequence of actions.

**ExampleThe Facts Were These
Creating a variant GIVE action that lets the player give multiple objects simultaneously with commands like GIVE ALL TO ATTENDANT or GIVE THREE DOLLARS TO ATTENDANT or GIVE PIE AND HAT TO ATTENDANT. The attendant accepts the gifts only if their total combined value matches some minimum amount.