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

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.