Puny Inform: How do I iterate over the contents of a container?

From what I can tell, Puny does not implement objectloop If I want to iterate over the contents of a container, what is the best way to do so?

Object box "box"
  with name 'box'
       before [;
           Close:
               objectloop (obj in self)
                   ! Do something to each object within
               rtrue;
       ],
  has container openable;

Objectloop is a part of Inform6, not a library function. It should work with Puny.

1 Like

Huh. Perhaps there’s something wrong with my syntax then?

Object box 'box'
  with
       before [;
           Close:
               objectloop (obj in self) {
                	print "Hello!";
	                rtrue;
                 };
       ],
  has container openable;

The compiler complains with:

line 38: Error:  Expected 'objectloop' variable but found obj
>                objectloop (obj
line 38: Error:  Expected ';' but found rtrue
>                  rtrue

Pretty sure you’ll need to declare the variable obj.

Object box 'box'
  with
       before [ obj ;
           Close:
               objectloop (obj in self) {
                	print "Hello!";
	                rtrue;
                 };
       ],
  has container openable;
1 Like

Yeah, I’ve already tried that. Ends up with another error. Which leads me to believe I’m using objectloop incorrectly.

"line 4: Warning:  Local variable "obj" declared but not used
```

That’s a warning (and correct because you don’t use obj inside the loop, only assign it).

EDIT: The loop, as it’s written, is a bit strange. It will print one Hello then return, never loop over all in self.

1 Like

This is perhaps a more typical form of usage:

Object -> Box "box"
	with
		name 'box',
		before [ obj;
			Close:
				objectloop (obj in self) {
					print "Box contains ", (a) obj, ".^";
                }
       ],
	has container open openable enterable;

Also note that there’s no need to put ; after }

2 Likes