I probably would have this in 5.8:
$ cat sentinel_58.pl #!perl use strict; use warnings; my @in = qw{start middle end}; my $i = 0; print map { join($_, ($i++ == $#in ? q{<li class="last">} : q{<li>}), q{</li>} +); } @in; $ sentinel_58.pl <li>start</li><li>middle</li><li class="last">end</li>
And here's much the same offering for newer Perl versions:
$ cat sentinel_512.pl #!perl use 5.12.0; use warnings; my @in = qw{start middle end}; say map { state $i = 0; join($_, ($i++ == $#in ? q{<li class="last">} : q{<li>}), q{</li>} +); } @in; $ sentinel_512.pl <li>start</li><li>middle</li><li class="last">end</li>
-- Ken
In reply to Re: Variant of map for special-casing the last item
by kcott
in thread Variant of map for special-casing the last item
by jdporter
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |