in reply to Re^3: Perl's feature to determine, in current point of loop, that this is the last one?
in thread Perl's feature to determine, in current point of loop, that this is the last one?
WRT use case: one of the (admittedly few) places I've seen something like this (which is why I mentioned the iterator module from TT) is in templating where you want to "decorate" the first or last item in a list differently than other items. Think maybe pagination where displaying a chunk from the middle and you want those items to have a link to the next or subsequent sets of results (terrible handwavy sample follows, and yes you could do similar stuff with JS or CSS instead).
<table> [% FOREACH item IN current_chunk %] <tr> [% IF loop.first %]<td> << </td>[% ELSE %]<td> </td>[% END +%] <td>[% item %]</td> [% IF loop.last%]<td> >> </td>[% ELSE %]<td> </td>[% END %] </tr> [% END %] </table>
Edit: And I realized I hadn't linked to the TT FOREACH docs which explain the loop magic (which is Template::Iterator under the hood).
The cake is a lie.
The cake is a lie.
The cake is a lie.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^5: Perl's feature to determine, in current point of loop, that this is the last one?
by talexb (Chancellor) on Jan 25, 2022 at 21:24 UTC | |
by afoken (Chancellor) on Jan 26, 2022 at 15:49 UTC |