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
skip an interation, how do you do it in perl?
Thierry
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";
}
}
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'
perl -e "$\=qq(\n); for (1..10) { next if $_==7; print }"
TIMTOWTDI
Or rather:
for (my $i = 0; $i < 10; ++$i)
{
if ($i == 7)
{
next;
}
print $i, "\n";
}
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";
}
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);
}
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.
And as a matter of Perlish style:
C++ 'for (my $i = 0; $i < 10; ++$i)' => Perl 'foreach my $i( 0..9 )'
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?
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
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
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.
But something like $ii or $ix is easier to search for whatever theMichele 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).
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...
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.