Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: validating file with perl

by farang (Hermit)
on Jul 16, 2013 at 22:15 UTC ( #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


Comment on Re: validating file with perl
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (11)
As of 2014-09-16 16:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (33 votes), past polls