Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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.

In reply to Re: Perl's feature to determine, in current point of loop, that this is the last one? (updated) by haukex
in thread Perl's feature to determine, in current point of loop, that this is the last one? by Anonymous Monk

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (3)
As of 2022-07-02 17:53 GMT
Find Nodes?
    Voting Booth?
    My most frequent journeys are powered by:

    Results (103 votes). Check out past polls.