Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

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

by ikegami (Patriarch)
on Jan 23, 2022 at 08:44 UTC ( #11140741=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?

Replace

for my $x ( ... ) { ... if ( ??? ) { ... } ... }
with
my @a = ...; for my $i ( 0 .. $#a ) { my $x = $a[$i]; ... if ( $i == $#a ) { ... } ... }

This will work too:

my @a = ...; for my $x (@a) { ... if ( \$x == \$a[-1] ) { ... } ... }

Warning: As LanX pointed out, the last snippet fails if two elements of the array are aliases of the same scalar.


Update: Added warning.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2022-05-23 11:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (82 votes). Check out past polls.

    Notices?