Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: foreach only for a section of a list

by brx (Pilgrim)
on May 04, 2012 at 15:19 UTC ( #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.


Comment on Re: foreach only for a section of a list
Select or Download Code
Re^2: foreach only for a section of a list
by johngg (Abbot) 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
Node Status?
node history
Node Type: note [id://968940]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (9)
As of 2013-05-25 18:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best material for plates (tableware) is:









    Results (520 votes), past polls