Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: validating file with perl

by farang (Chaplain)
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 meditating upon the Monastery: (6)
As of 2015-07-07 06:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (87 votes), past polls