G'day ler224,

Firstly, some issues I had with your question:

Here's a technique that uses my array of arrays assumption: you can probably modify it for some other data structure (or to take unshown data into account) if my assumption is wrong.

You'll see there's no constraint that the inner arrays must have six elements nor additional code targetting specific indices. This is intended to both reduce the amount of code you need to write (and maintain) as well as allowing the number of elements to be modified at some future time without requiring changes to the code.

Also note I've added four more test cases: [2,3] (contains neither success or fail cases); [1,1,1,2] (contains both success and fail cases); [] (contains no cases at all); and, [3,2,1] (bad data - odd number of elements).

#!/usr/bin/env perl -l use strict; use warnings; my @data = ( [1,1,,,,], [1,2,,,,], [3,4,1,1,,], [1,1,1,1,,], [5,6,3,4,1,2], [1,1,,,,], [1,1,1,1,1,1], [2,3], [1,1,1,2], [], [3,2,1], ); my %lines_with = (success => 0, fail => 0); for my $line (@data) { my ($success, $fail) = (0, 0); while (@$line) { my ($first, $second) = splice @$line, 0, 2; next unless defined $first && $first == 1; next unless defined $second; ++$success if $second == 1; ++$fail if $second == 2; } ++$lines_with{success} if $success; ++$lines_with{fail} if $fail; } print "Lines with successes: $lines_with{success}"; print "Lines with fails: $lines_with{fail}";

Output:

Lines with successes: 6 Lines with fails: 3

-- Ken


In reply to Re: Argument "" Isn't numeric in numeric eq (==) by kcott
in thread Argument "" Isn't numeric in numeric eq (==) by ler224

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.