Creating sentences out of Repeat throughs

So I know how to create lists using Repeats.
For example:

say "Items in this room:";
Repeat with ItemX running through Items in the location of the player:
	say "- [ItemX][line break]";

That basically gives you:

Items in this room:

  • a hammer
  • a knife
    etc

I would like to form a sentence with it however.
So i’ve started with this…

Let ItemsInRoom be "";
Repeat with ItemX running through Items in the location of the player:
	if ItemsInRoom is "": [First Item]
		Now ItemsInRoom is "a [ItemX]";
        otherwise: [Successive Items]
		Now ItemsInRoom is "[ItemsInRoom], a [ItemX]"; [Build the sentence string of items]
say "Looking around the room you see [ItemsInRoom]";

That would work for the most part but what I need is a way to know when the last item is being dealt with, so that the last item can be printed as:

say "[ItemsInRoom] and a [ItemX]";

That way if there was 3 items for example the sentence would be:

Looking around the room you see a Hammer, a Knife and a Notepad.

In short, anyway of catching the last item in a repeat?

No.

However, there’s a powerful lister already built into Inform. See chapter 6.1. You can write

say "Looking around the room, you see [the list of Items in the location of the player].";

(Assuming that you’ve defined Item as a kind of thing.)

You could use a list X that varies, fill it with the objects in the location, set some other variable Y to equal the last entry in this list X, remove the last entry from X, cycle through X, and print “and [Y]” at the end. However, this would just be an enormously complicated way of doing just what Inform does for you when you use zarf’s syntax. :slight_smile:

Not directly, but if you work out the number of repeats beforehand:

Let Nunber_of_things be the number of things in the location of the player;		
Let ItemsInRoom be "";
Let Counter be 0;
Repeat with ItemX running through things in the location of the player:
	increment Counter;
	if Counter is 1: [First Item]
			Now ItemsInRoom is "a [ItemX]";
		otherwise if Counter is Number_of_things: [Last Item, if there are 2 or more]
			Now ItemsInRoom is "[ItemsInRoom], and a [ItemX]";
		otherwise: [Successive Items]
			Now ItemsInRoom is "[ItemsInRoom], a [ItemX]"; [Build the sentence string of items]
	say "Looking around the room you see [ItemsInRoom].";

You could also do away with the number_of_things variable and just write

		otherwise if Counter is the number of things in the location of the player:

if you don’t mind Inform having to work this out for every iteration of the repeat.

Although for this particular purpose, you can of course use the built-in list maker…

1 Like

I ended up using a method very similiar to Dr Peter Bates suggestion.
Having a counter compared to the amount of items in room that when reached the same total printed a different item string.

The Built-In List is good to know for the future.
I tried it and while it definately built a sentence out of the items in the room, it wasn’t perfect english.

Looking around you see Hammer, Knife and Notepad.

I would prefer it to say:

Looking around you see a Hammer, a Knife and a Notepad.

For that i’ll need to assemble the string by hand.
The counter does the job!

Your counter idea is great, and sounds cool!

To change the articles in the default ‘list’ command, you add them in. So if you say:

[list of blah blah blah] it prints ‘hammer, knife, notepad’.

If you say

[a list of blah blah blah] it prints ‘a hammer, a knife, a notepad’.

If you say

[the list of blah blah blah] it prints ‘the hammer, the knife, the notepad’.

I’m glad you found your own solution, I always find that fun to do.

1 Like

Furthermore, the built-in list writer will automatically take care of using a/an appropriately, any plural-named things (‘some grapes’ instead of ‘a grapes’), proper-named things (‘Mr Darcy’ instead of ‘a Mr Darcy’) and things with specified indefinite articles (‘several ounces of butter’ instead of ‘a butter’), all of which would require additional coding to expand the simple proof-of-concept given above into a full general-purpose solution. Of course, if no objects like this exist in your story, there’s no need to worry too much about these subtleties… :wink: