use strict; use warnings; use v5.12; my @array = ( [ qw/ 1 0 1 0 1 / ], [ qw/ 0 1 0 1 0 / ], [ qw/ 0 0 1 1 0 / ], [ qw/ 0 0 0 0 1 / ], [ qw/ 0 0 0 0 0 / ], ); if( defined( my $result = find_first_sideways( \@array ) ) ) { say "First row containing all zeros is $result."; } else { say "No rows found to contain all zeros."; } sub find_first_sideways { my $matrix = shift; my @scan = ( 0 .. $#{ $matrix } ); my $depth = 0; while( @scan > 0 and $depth < @{ $matrix->[0] } ) { @scan = grep{ $matrix->[$_][$depth] == 0 } @scan; $depth++; } if( defined( $scan[0] ) ) { return $scan[0]; } else { return undef; } }