http://www.perlmonks.org?node_id=927686


in reply to An exploration of higher-level-language approaches to the "First all-zero row in an NxN matrix" problem (goto Considered Harmful)

In Perl we frown on the old C style for loop because almost always there is a better way to do it with a Perl foreach style loop. However, not always. The iterator variant below uses a C style loop to remember the last column and last row dealt with to allow a simple last to short circuit the loops with the state of the loop counter propagating the success or failure.

my $i; for ($i = 0; $i < @array; ++$i) { my $row = $array[$i]; my $j; for ($j = 0; $j < @$row; ++$j) { last if $row->[$j]; } last if $j == @$row; } print "All zero row at $i\n" if $i != @array;

Given CountZero's test matrix prints:

All zero row at 3
True laziness is hard work
  • Comment on Re: An exploration of higher-level-language approaches to the "First all-zero row in an NxN matrix" problem (goto Considered Harmful)
  • Select or Download Code