Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: foreach only for a section of a list

by kennethk (Abbot)
on May 04, 2012 at 15:01 UTC ( #968934=note: print w/ replies, xml ) Need Help??


in reply to foreach only for a section of a list

There are a few ways. You can use C-style For Loops:

for (my $i = 0; $i < @somewords -1; $i++) { print " $somewords[$i];\n" }
You can use Slices:

foreach $core (@somewords[0 .. $#somewords-1]) { print " ${core};\n" }
Or, in some cases, it makes sense to use Loop Control:

my $i = 0; foreach my $core (@somewords) { next if ++$i == @somewords; print " ${core};\n" }

TIMTOWTDI

#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.


Comment on Re: foreach only for a section of a list
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2015-07-02 00:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (25 votes), past polls