in reply to Variant of map for special-casing the last item
where the first sub is the default, and last (or $#list) is applied only for the last element.my @list_in_html = map_by_index [ sub { "<li>$_</li>" }, last => sub { qq!<li class="last">$_</li>! }, ], @list; my @list_in_html = map_by_index [ sub { "<li>$_</li>" }, $#list => sub { qq!<li class="last">$_</li>! }, ], @list;
Or using List::MapList (which could be optimized)
my @list_in_html = maplist( [ ( sub { "<li>$_</li>" } ) x ( @list - 1 ), sub { qq!<li class="last">$_</li>! }, ], @list );
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Variant of map for special-casing the last item
by jdporter (Paladin) on Oct 25, 2010 at 16:09 UTC | |
by Anonymous Monk on Oct 25, 2010 at 19:24 UTC |