![]() | Chapter 20: Lists | ![]() ![]() |
20.4. Testing and iterating over lists |
If L is a list, we can interrogate it to see whether it does or does not contain (at least one instance of) any compatible value V:
if V is listed in L, ...
if V is not listed in L, ...
As always, V must be a value compatible with the entries of L, or else the question is meaningless and Inform will raise a problem message. For instance, if L is our list of the numbers 2, 3, 5, 7 and 11 then 5 is listed in it but 6 is not.
We can also repeat running through a list (just as we can with table rows). Thus:
repeat with item running through L:
...
repeats "..." for each entry of L, with item set to the value of that entry. (Thus if L is a list of numbers then "item" will be a number variable, and so on.) If the list is empty, nothing happens: the "..." phrase(s) are never tried.
In the next sections, we shall see that it is possible to change, reorder and resize lists. Never change a list that's being repeated through. The following:
let L1 be {1, 2, 3, 4};
repeat with n running through L1:
remove n from L1;
leaves L1 containing {2, 4}, since the removals from the list cause it to shuffle back even while we repeat through it - a bad idea.
Previous | Contents | Next |