Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: validating file with perl

by farang (Chaplain)
on Jul 16, 2013 at 22:15 UTC ( [id://1044668]=note: print w/replies, xml ) Need Help??


in reply to validating file with perl

I'm sure there are more elegant and probably better solutions, but this is one way to do it.

#!/usr/bin/env perl use strict; use warnings; my %fields; my $header = <DATA>; while (<DATA>) { chomp; next if /\A\s*\Z/; my ($f1, $f2) = split; push @{ $fields{$f1} }, $f2; } for my $k (keys %fields) { my ($one, $three) = (0, 0); # if either of these remain zero, ID i +s invalid for my $values ( @{ $fields{$k} } ) { ++$one if ( $values == 1 ); ++$three if ( $values == 3 ); } print "invalid ID: $k\n" unless $one * $three; } __DATA__ field1 field2 3-2000/7.48 1 3-2000/7.48 2 3-2000/7.48 2 3-2000/7.48 2 3-2000/7.48 2 3-2000/7.48 2 3-2000/7.48 2 3-2000/7.48 3 4-0000/8.40 2 4-0000/8.40 2 4-0000/8.40 2 4-0000/8.40 2 4-0000/8.40 2 4-0000/8.40 2 4-0000/8.40 3

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-19 15:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found