Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: foreach only for a section of a list

by brx (Pilgrim)
on May 04, 2012 at 15:19 UTC ( [id://968940]=note: print w/replies, xml ) Need Help??


in reply to foreach only for a section of a list

Another way, with pop and push:

my $last = pop @somewords; foreach my $core (@somewords) { print " $core;\n" } push @somewords,$last;

Update: it's useless to keep $last. See the improvement by johngg, in reply.

Replies are listed 'Best First'.
Re^2: foreach only for a section of a list
by johngg (Canon) on May 05, 2012 at 12:13 UTC

    You could use a do block to scope the existence of $last so that it isn't left lying around.

    knoppix@Microknoppix:~$ perl -Mstrict -wE ' > my @arr = qw{ one two three four }; > push @arr, do { my $last = pop @arr; say for @arr; $last }; > say qq{@arr};' one two three one two three four knoppix@Microknoppix:~$

    Cheers,

    JohnGG

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-18 17:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found