Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Perl's feature to determine, in current point of loop, that this is the last one?

by talexb (Chancellor)
on Jan 23, 2022 at 20:45 UTC ( [id://11140749]=note: print w/replies, xml ) Need Help??


in reply to Perl's feature to determine, in current point of loop, that this is the last one?

You could always watch for the last value in the list and test for that ..

my @values = qw/red green brown blue/; foreach my $v ( @values ) { if ( $v eq $values[-1] ) { # Last value } else { # All other values } }
As you've already heard, there's nothing built in to Perl that handles that.

Alex / talexb / Toronto

Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

  • Comment on Re: Perl's feature to determine, in current point of loop, that this is the last one?
  • Download Code

Replies are listed 'Best First'.
Re^2: Perl's feature to determine, in current point of loop, that this is the last one?
by LanX (Saint) on Jan 24, 2022 at 02:19 UTC
    > You could always watch for the last value in the list and test for that ..

    this only works if the last value is unique

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

      Absolutely right -- I knew that my approach had flaws, but the initial question was so vague I figured I'd throw my solution into the mix. The OP never explained why this feature was necessary .. it sounds more like a feature looking for a use case to me.

      Alex / talexb / Toronto

      Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

        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> &lt;&lt; </td>[% ELSE %]<td>&nbsp;</td>[% END +%] <td>[% item %]</td> [% IF loop.last%]<td> &gt;&gt; </td>[% ELSE %]<td>&nbsp;</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.

        It's so easy to avoid the limitation. Replace $v eq $values[-1] with \$v == \$values[-1]

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11140749]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-19 16:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found