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

    I agree that ler224's OP is no more than a hint of a suggestion of a specification, but the only way I can see to get an array containing empty strings from something like  1,1,,,, is if it's a string  '1,1,,,,' that's been subjected to some kind of split operation. If  1,1,,,, is a list literal, all those commas flatten to nothing.

    c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my @ra = ([1,1], [1,1,,,,], [1,1,,,,,,,,,,,,]); dd \@ra; " [[1, 1], [1, 1], [1, 1]]

      While I don't disagree with you, I think we could guess at the data structure and missing code until the cows come home and still get it wrong.

      My intent was really just meant to provide an idea of how the task might be tackled:

      "Here's a technique ... you can probably modify it ..."

      [I also note that it's more than a day and half since the OP was posted and, despite a number of replies raising issues with it, ler224 has provided no clarification whatsoever.]

      -- Ken

        I also saw that the only reply by ler224 was to CountZero's first reply and only very shortly after the timestamp of the OP — and it did not address the pertinent part of the Count's suggestion! Not the sort of engagement one hopes to see.