Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Argument "" Isn't numeric in numeric eq (==)

by Kenosis (Priest)
on Feb 09, 2014 at 20:31 UTC ( [id://1074161]=note: print w/replies, xml ) Need Help??


in reply to Argument "" Isn't numeric in numeric eq (==)

Consider using a regex that captures sets of two digits (or 'empties') separated by a comma, as tuples:

use strict; use warnings; while ( my $line = <DATA> ) { print +( join ',', map "($_)", $line =~ /(\d*,\d*),?/g ), "\n"; } __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 ,,1,2,, ,,1,1,1,2 ,,,,, 5,6,3,4,2,2

Output:

(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) (,),(1,2),(,) (,),(1,1),(1,2) (,),(,),(,) (5,6),(3,4),(2,2)

And then testing for '1,1' and '1,2' in each line:

use strict; use warnings; my ( $i, $success, $fail ); while ( my $line = <DATA> ) { $i++; my %tuples = map { $_ => 1 } $line =~ /(\d*,\d*),?/g; $success++ if exists $tuples{'1,1'}; $fail++ if exists $tuples{'1,2'}; } print "Total Lines: $i\nSuccess: $success\nFail: $fail\n"; __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 ,,1,2,, ,,1,1,1,2 ,,,,, 5,6,3,4,2,2

Output:

Total Lines: 11 Success: 6 Fail: 4

BTW - You were off by one in all of your array indices. You had:

if((($f[1]==1)&&($f[2]==1))|| ...

when you meant:

if((($f[0]==1)&&($f[1]==1))|| ...

Hope this helps!

Edit: Updated to reflect the '1,2' "Fail" condition, which I failed to notice. Thank you, Jim.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1074161]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-03-28 16:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found