On Fri, Apr 26, 2002 at 08:49:23AM +1000, Damian Conway wrote:
> for $results.get_next() {
> FIRST { print "Results:<BR>"; }
> NEXT { print "<HR>"; }
> LAST { print "Done."; }
> print $_;
> }
How about something like this:
for $results.each() {
print "Results:<BR>" if $results.first;
print $_;
print $results.last ? "Done." : "<HR>";
}
or perhaps
foreach $item ($results.each) {
print "Results:<BR>" if $item.first;
print $_;
print $item.last ? "Done." : "<HR>";
}
Calling each() on a list (or a hash?) should somehow transmogrigfy it,
or provide an attribute interface around it, which makes it look more
like a classic iterator. The psuedo-iterator can implement methods like
first(), last(), index(), count(), size(), max() and so on for providing
information about, and control over loops.
This is an approach I've used to great effect in the Template Toolkit.
In this case, the iterator controlling a 'FOREACH' loop is aliased to
the 'loop' variable
[% FOREACH x = y %]
[% "<table>\n" IF loop.first %]
<tr>
<td>[% loop.count %]</td>
<td>[% x.key %]</td>
<td>[% x.val %]</td>
</tr>
[% "</table>\n" IF loop.last %]
[% END %]
Builtin support for iterators can make difficult things easy. Ruby's
implementation is worth a look for inspiration (as indeed is most of the
Ruby language :-)
A