in reply to Argument "" Isn't numeric in numeric eq (==)
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
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Argument "" Isn't numeric in numeric eq (==)
by AnomalousMonk (Archbishop) on Feb 10, 2014 at 12:30 UTC | |
by kcott (Archbishop) on Feb 11, 2014 at 04:52 UTC | |
by AnomalousMonk (Archbishop) on Feb 11, 2014 at 12:15 UTC |