Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

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

by haukex (Bishop)
on Jan 23, 2022 at 08:32 UTC ( #11140740=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?

meant is there some Perl's simple internal variable or feature, not resort to some added line code/variable must be done by coder ?

Having wished for this myself, I can say that the answer to the general question is unfortunately: No, with the exception of eof when reading from files, as mentioned by kcott.

TIMTOWTDI, though. You haven't shown any example code so it's really hard to say which variation below (if any) might be most appropriate or easiest in your case.

use warnings; use strict; my @array = qw/ Foo Bar Quz /; # C-style loop, not very perlish (but *sometimes* useful) for ( my $i = 0; $i < @array; $i++ ) { print $array[$i], $i==$#array ? ".\n" : ", "; } # the perlish variant of the above for my $i (0..$#array) { print $array[$i], $i==$#array ? ".\n" : ", "; } use 5.012; # so keys/values/each work on arrays while ( my ($i, $v) = each @array ) { print $v, $i==$#array ? ".\n" : ", "; } # as suggested by jwkrahn my $cnt = @array; for my $v (@array) { print $v, --$cnt ? ", " : ".\n"; } # or sometimes it's as simple as "join" print join(", ", @array), ".\n"; my @array_copy = @array; # because it's destructive! while ( my $v = shift @array_copy ) { print $v, @array_copy ? ", " : ".\n"; }

And lastly, while it might seem overkill, the concept of iterators can be extremely useful in some cases. The book Higher-Order Perl is a great read on this and other topics. Here, I've extended the concept with a "peekable" iterator class. One can even overload the <> operator as I showed here (note one does need Perl v5.18 or better for the overloaded <> to work in list context). Update: In the following code, I've replaced my own classes with Iterator::Simple and Iterator::Simple::Lookahead. /Update

# demo of a basic iterator use Iterator::Simple 'iter'; my $it = iter(\@array); while (defined( my $v = $it->() )) { print $v, " "; } print "\n"; # demo the peekable iterator use Iterator::Simple::Lookahead; my $itp = Iterator::Simple::Lookahead->new( iter(\@array) ); while (defined( my $v = $itp->() )) { print $v, defined $itp->peek ? ", " : ".\n"; }

Bonus: For some experimental stuff, see FIRST and LAST in Perl6::Controls.

Also minor edits to wording.

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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2022-05-25 15:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (90 votes). Check out past polls.

    Notices?