# Re: use of next versus last in a nested loop algorithm

Paul Lalli wrote:
markpark wrote:
if there is a match, i want to get
out of the foreach my \$pat loop and go to the next \$item in the
foreach my \$item loop on the outside. i've read about next and last in
various books and still i can't tell what to use because
my case is a little different than the examples in the books because i
am comparing values in
the two loops to decide when to get out. therefore, i don't think
whatever statement i use, next
or last, can go in the if statement ?

foreach my \$item (@filelist) {
foreach my \$pat (@datelist2array) {
if ( \$item =~ /\$pat/ ) {
print \$out_file2 "\$item \n";
}
}
}

if() {} is not a looping construct, and therefore next and last have no
effect on it. If you put a next; inside the if statement, that will
start the next iteration of the inner for loop. If you put a last;
inside the if statement, that will end the inner for loop.

If you're really paranoid about what loop a next/last applies to, label

ITEM: foreach my \$item(@filelist) {
PAT: foreach my \$pat (@datelist2array) {
if (\$item =~ /\$pat/) {
print \$out_file2 "\$item \n";
next ITEM;
#or, equivalently in this case
# last PAT;
}
}
}

it's not just for the paranoid, Paul. it's a Best Practice. :-D

-jp

PS: loop labels on the preceding line :-P
PPS: you will be assimilated.

.

## Relevant Pages

• Re: use of next versus last in a nested loop algorithm
... out of the foreach my \$pat loop and go to the next \$item in the ... foreach my \$item loop on the outside. ... that will end the inner for loop. ...
(comp.lang.perl.misc)
• Re: Count Lines in (Huge) Text Files
... A few years ago, I was doing some high-throughput disk stuff and my recollection is that I found the same thing you did: larger buffers only helped up to about 8K or so, and past that any improvement was minimal. ... me that with appropriate settings for its buffer, it should perform better, since it ought to be optimized for line-based i/o. ... Assuming what's hurting you in the explicit forloop is the retrieval of the data and not the counter increment, the above should perform basically as well as a plain foreach() loop. ...
(microsoft.public.dotnet.languages.csharp)
• Re: a question about for and foreach
... foreach (@array) { ... why @array are changed after this foreach loop!!! ... The "foreach" loop iterates over a normal list value and sets the ... If any element of LIST is an lvalue, you can modify it by modifying ...
(perl.beginners)
• Re: Lost data on socket - Can we start over politely?
... \$clsel as a single-element-holding object containing the current ... simply performing the following check on \$sel instead of \$clsel? ... removing elements from something from a list used for the loop is a Bad ... Doesn't that throw the foreach() loop out of sync? ...
(comp.lang.perl.misc)
• Re: Re: Last row in foreach loop
... foreach loop other then setting up a loop counter that you manually ... foreach (Racecar racecar in RaceCarCollection) ... almost as reasonable to put the processing into a separate method and th= ...
(microsoft.public.dotnet.languages.csharp)