How do you continue in a for loop?

T

Thierry

In other languages, the for loop has a continue statement feature to
skip an interation, how do you do it in perl?

Thierry
 
A

Azazel

In other languages, the for loop has a continue statement feature to
skip an interation, how do you do it in perl?

"next". For example:

for (my $i = 0; $i < 10; ++$i)
{
if ($i == 7)
{
next;
}
else
{
print $i, "\n";
}
}
 
A

Azazel

"next". For example:

for (my $i = 0; $i < 10; ++$i)
{
if ($i == 7)
{
next;
}
else
{
print $i, "\n";
}
}

Or rather:

for (my $i = 0; $i < 10; ++$i)
{
if ($i == 7)
{
next;
}

print $i, "\n";
}
 
P

Paul Lalli

In other languages, the for loop has a continue statement feature to
skip an interation, how do you do it in perl?

Thierry

C++ 'continue' ==> Perl 'next'
C++ 'break' ==> Perl 'last'
C++ ???? ==> Perl 'redo' (restarts the current iteration)

Paul Lalli
 
U

usenet

C++ 'continue' ==> Perl 'next'
C++ 'break' ==> Perl 'last'

And as a matter of Perlish style:

C++ 'for (my $i = 0; $i < 10; ++$i)' => Perl 'foreach my $i( 0..9 )'

(you can use 'for' if you like - I find 'foreach' reads better)

And, of course, $i is a terrible variable name.
 
M

mirod

perl -e "$\=qq(\n); for (1..10) { next if $_==7; print }"

TIMTOWTDI

Well, if you're going that route, the -l option is worth having a look
at:

perl -l -e 'for (1..10) { next if $_==7; print }'
 
M

Michele Dondi

Or rather:

for (my $i = 0; $i < 10; ++$i)
{
if ($i == 7)
{
next;
}

print $i, "\n";
}

Well said, in fact next, last and redo often make for more agile
syntax and exactly serve the purpose of avoiding the need for explicit
else's. Better yet, to further expand on the subject, such simple
cases can be cast in the form of a statement modifier:

for (my $i = 0; $i < 10; ++$i) {
next if $i == 7;
print $i, "\n";
}

Incidentally, a Perl-style C<for> is clearer and generally preferred:

for my $i (0..9) {
next if $i == 7;
print $i, "\n";
}

Well, and in this case the loop block is so small that one can safely
go with the topicalizer rather than with an explicit lexical variable:

for (0..9) {
next if $_ == 7;
print $_, "\n"; # Grr! .say
}

(Of course the example was just that: and example, and with bigger
blocks it is definitely better the other way round.)


Michele
 
J

Jürgen Exner

Michele said:
Incidentally, a Perl-style C<for> is clearer and generally preferred:

for my $i (0..9) {
next if $i == 7;
print $i, "\n";
}

Maybe it is just me but I have never been comfortable with this style of
programming. When I see a loop like that I assume that the loop will execute
exactly 10 times just as specified in the loop condition. To me this "next"
and "last" feels like cheating, like I wasn't smart enough to come up with
the correct loop condition.
IMHO it also makes for harder to maintain code because when reading the code
you need to revise your assumptions about how the loop is executed somewhere
in the middle of the loop body.

In the case at hand what is wrong with a simple

for my $i (0..6, 8..9) {
print $i, "\n";
}

jue
 
M

Michele Dondi

Maybe it is just me but I have never been comfortable with this style of
programming. When I see a loop like that I assume that the loop will execute

Well *of course* it's just you. Just as obviously some will agree with
you and some will disagree.
exactly 10 times just as specified in the loop condition. To me this "next"
and "last" feels like cheating, like I wasn't smart enough to come up with
the correct loop condition.

This example is actually artificial and one doesn't need C<next> at
all:

$_ != 7 and print for 0..9; # assuming $\="\n";

But then you may like this one even the less.
IMHO it also makes for harder to maintain code because when reading the code
you need to revise your assumptions about how the loop is executed somewhere
in the middle of the loop body.

To put it briefly there are situations in which one kind of approach
is preferred and situations in which the other one is.
(Un?)fortunately the distinction is blurred.
In the case at hand what is wrong with a simple

for my $i (0..6, 8..9) {
print $i, "\n";
}

Nothing's wrong, but then what if the "seven" condition is to change?
You would have to change in two places, albeit contiguous. Of course
you could do something like the following instead:

for my $i (0..$n-1, $n+1..9) {

But then if not except for matters of performance, I would rather do

for my $i (grep $_ != $n, 0..9) {

which is IMHO clearer. And we could discuss so on for hours. Just
stories of ordinary TMTOTDIness...


Michele
 
J

Jürgen Exner

Lawrence said:
Only because this is a trivailized example -- in "real" programming,
one does not iterate across a list just for the joy of doing something
"n" times - but there is some task you're completing...

for my $bridesmade (@bridesmaids) {
# all bridesmaids need some stuff
buy_gift_for($bridesmaid);
dye_shoes_for($bridesmaid);
next unless lives_in_other_city($bridesmaid);
# but only the ones that are from another city need ...
rent_hotel_room_for($bridesmaid);
book_travel_plans_for($bridesmaid);
}

To _ME_ it seems more logical and easier to write

for my $bridesmade (@bridesmaids) {
# all bridesmaids need some stuff
buy_gift_for($bridesmaid);
dye_shoes_for($bridesmaid);

if (lives_in_other_city($bridesmaid)) {
# but only the ones that are from another city need ...
rent_hotel_room_for($bridesmaid);
book_travel_plans_for($bridesmaid);
}
}
One must strive to write code that is only as complex as it needs to
be...Often, a "next" or "last" inside the loop to handle exceptional
conditions is needed.

For exceptional circumstances I actually agree. It doesn't do well to
clutter the main flow of your code with exceptions that occur once in a
million.

jue
 
A

Azazel

And as a matter of Perlish style:

C++ 'for (my $i = 0; $i < 10; ++$i)' => Perl 'foreach my $i( 0..9 )'

Fair point. I was up to my eye-balls in C when I wrote that.

Az.
 
I

Ian Wilson

Michele said:
This example is actually artificial and one doesn't need C<next> at
all:

Where'd this C<...> quoting style come from? I've seen it in several
postings. Does it do something useful in some newsreaders?
 
M

Michele Dondi

Where'd this C<...> quoting style come from? I've seen it in several
postings. Does it do something useful in some newsreaders?

Not at all, it's POD format. Generally in a pure text context like
this one, if a "keyword" is visually distinctive enough then I see no
need to mark it in any special way. For example if I refer to $_ as
you can see I don't put anything around it; for functions, a pair of
parens to disambiguate are enough, e.g. map(); but with other stuff
that may confuse with normal text I go pseudo-POD e.g. with C<for>.
See

perldoc perlpod


Michele
 
I

Ian Wilson

Abigail said:
_
Ian Wilson ([email protected]) wrote on VLXXXIV September MCMXCIII
in <URL:-: Michele Dondi wrote:
-: > This example is actually artificial and one doesn't need C<next> at
-: > all:
-:
-: Where'd this C<...> quoting style come from? I've seen it in several
-: postings. Does it do something useful in some newsreaders?


man perlpod

Thanks,

=begin html
<p>I'm <strong>not</strong> in favour of using markup in newsgroups but
I suppose I&apos;ll have to get used to the <em>clutter</em> ;-)</p>
=end html
 
I

Ian Wilson

Michele said:
Not at all, it's POD format. Generally in a pure text context like
this one, if a "keyword" is visually distinctive enough then I see no
need to mark it in any special way. For example if I refer to $_ as
you can see I don't put anything around it; for functions, a pair of
parens to disambiguate are enough, e.g. map(); but with other stuff
that may confuse with normal text I go pseudo-POD e.g. with C<for>.
See

perldoc perlpod

Thanks, I think I'll stick with 'for', "next" or `perl`, but to each his
own. :)
 
I

Ingo Menger

And, of course, $i is a terrible variable name.

On the contrary.
It's short.
It's a clichee and as such conveys information about the intent of the
programmer. I for one use it *only* for integer valued indexes or loop
variables, and insofar $i is much better than everbodies whoore $_,
which can be anything anywhere.
 
M

Michele Dondi

On the contrary.
It's short.
It's a clichee and as such conveys information about the intent of the
programmer. I for one use it *only* for integer valued indexes or loop
variables, and insofar $i is much better than everbodies whoore $_,
which can be anything anywhere.

$_ is a pronoun. $i is a noun. Each is good for what it's good, don't
talk so bad 'bout $_ please. Anyway I agree with you: $i is perfectly
fine for integer indexes.


Michele
 
M

Mark Clements

Michele said:
$_ is a pronoun. $i is a noun. Each is good for what it's good, don't
talk so bad 'bout $_ please. Anyway I agree with you: $i is perfectly
fine for integer indexes.
But something like $ii or $ix is easier to search for whatever the
editor (much less worrying about word boundaries and so forth).

Mark
 
M

Michele Dondi

But something like $ii or $ix is easier to search for whatever the
editor (much less worrying about word boundaries and so forth).

Well, if you use either $i or $_ for anything that may require a
search with your text editor, then you're on your own. And in that
case $ii or $ix may not be that good either...


Michele
 
I

Ian Wilson

Michele said:
Well, if you use either $i or $_ for anything that may require a
search with your text editor, then you're on your own. And in that
case $ii or $ix may not be that good either...

Someone once recomended small names for small scopes, big names for big
scopes. I find that a useful maxim.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,294
Messages
2,571,511
Members
48,218
Latest member
NatishaFin

Latest Threads

Top