Using List::Util->sum() and grep seems to work.
knoppix@Microknoppix:~$ perl -MList::Util=sum -E ' > @array = ( > [ qw{ 1 1 1 1 1 } ], > [ qw{ 0 1 1 0 0 } ], > [ qw{ 0 0 0 0 1 } ], > [ qw{ 0 0 0 0 0 } ], > [ qw{ 0 1 0 1 0 } ], > [ qw{ 0 0 0 0 0 } ], > ); > say qq{First all-zero row is @{ [ > ( grep { ! sum @{ $array[ $_ ] } } 0 .. $#array )[ 0 ] ] }};' First all-zero row is 3 knoppix@Microknoppix:~$
I hope this is of interest.
Update: I'm stupid! It's even simpler to use List::Util->first() instead of grep.
knoppix@Microknoppix:~$ perl -MList::Util=sum,first -E ' > @array = ( > [ qw{ 1 1 1 1 1 } ], > [ qw{ 0 1 1 0 0 } ], > [ qw{ 0 0 0 0 1 } ], > [ qw{ 0 0 0 0 0 } ], > [ qw{ 0 1 0 1 0 } ], > [ qw{ 0 0 0 0 0 } ], > ); > say q{First all-zero row is }, > first { ! sum @{ $array[ $_ ] } } 0 .. $#array;' First all-zero row is 3 knoppix@Microknoppix:~$
Update 2: Stupid and naive :-( List::Util->sum() is not a good tool as it will fail on something as simple as the new second row below. Using a List::Util->first() within another List::Util->first() might be better.
knoppix@Microknoppix:~$ perl -MList::Util=first -E ' > @array = ( > [ qw{ 1 1 1 1 1 } ], > [ qw{ 1 0 0 -1 0 } ], > [ qw{ 0 1 1 0 0 } ], > [ qw{ 0 0 0 0 1 } ], > [ qw{ 0 0 0 0 0 } ], > [ qw{ 0 1 0 1 0 } ], > [ qw{ 0 0 0 0 0 } ], > ); > > $found = first { > $i = $_; > $res = first { > $array[ $i ]->[ $_ ] != 0 > } 0 .. $#{ $array[ $i ] }; > ! defined $res; > } 0 .. $#array; > say > q{First all-zero row is -> }, > defined $found > ? $found > : q{none found}; > ' First all-zero row is -> 4 knoppix@Microknoppix:~$
Cheers,
JohnGG
In reply to Re: An exploration of higher-level-language approaches to the "First all-zero row in an NxN matrix" problem (goto Considered Harmful)
by johngg
in thread An exploration of higher-level-language approaches to the "First all-zero row in an NxN matrix" problem (goto Considered Harmful)
by davido
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |