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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.