Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

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" }


#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?

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (9)
As of 2015-08-30 16:15 GMT
Find Nodes?
    Voting Booth?

    The oldest computer book still on my shelves (or on my digital media) is ...

    Results (350 votes), past polls