Is there a way to target the index or the first or last item while running through things?
something like:
...
repeat with item running through things:
if the item is x index:
say "this is item number [x index]";
if the item is last:
say "this is the last item";
Even if I did that, how do I compare the index to the length of the list? Do I need to build that too somehow or is there already an equivalent of something like “something.length” in Inform that can be applied to a list?
The usual approach is to maintain the index manually:[code]There is a room; there is a container; there is a supporter; there is a device.
When play begins:
let the ultimate index be the number of things [as Draconis suggested] minus one;
let the index be zero;
repeat with the item running through things:
say “[The item].”;
if the index is two:
say “-- This ([the item]) is item number two.”;
if the index is the ultimate index:
say “-- This ([the item]) is the last item.”;
increment the index.[/code]
Edit: Okay, you people already got three posts in while I was writing this. Further edit: Fixed a silly spelling error.
I’m having trouble getting this to work in the context I originally encountered the issue for. This code was drawn up in a different thread I opened, but I thought the issue here (dealing with lengths of lists) was specific enough for its own thread.
How can I insert index checking and last into this?
To say descriptive (L - list of objects):
repeat with the item running through L:
say "[description of the item]".
I tried a similar syntax to what you have there, eu, but it doesn’t like it (says it can’t understand “number of objects in L”, but I don’t know what the proper syntax for referencing that would be):
To say descriptive (L - list of objects):
let the ultimate index be the number of objects in L minus one;
let index be 0;
repeat with the item running through L:
if the index is the ultimate index:
say " and [description of the item]";
otherwise:
say "[description of the item], ";
You don’t necessarily need to increment the index manually:
To say descriptive (L - list of objects):
repeat with index running from 1 to the number of entries in L:
if the index is the number of entries in L:
say " and [description of entry index in L]";
otherwise:
say "[description of entry index in L], ".