in reply to Re^7: loop surprise
in thread loop surprise
If the purpose of the loop is to find an index, then I would try to make that fact prominent at the start of the loop. That way you don't have to read the body of the code to figure out the purpose/intent.my $interesting_index; my $i = 0; while (!defined($interesting_index) and $i++ <= 10) { $interesting_index = $i if condition($i); } # spit an error if $interesting_index is still 'not defined'
In Perl often there are other ways to do something similar to this functionality with the various List::utils. My Perl code rarely uses any $i subscripts.
update: maybe I have an "off by one" error in original post. I think it is also possible to use the C style loop:
for (my $i=1; !defined($interesting_index) and $i<=10; $i++){}
|
|---|