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 ( [id://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?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-19 04:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found