Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Perl: How to perfectly match specific data between two files and do comparison?

by poj (Priest)
on Jul 23, 2013 at 18:39 UTC ( #1045934=note: print w/ replies, xml ) Need Help??


in reply to Perl: How to perfectly match specific data between two files and do comparison?

What is result if File A score is 50 and File B score is 40 ?. Assuming that anything that isn't NC or PASS is FAIL then your code can be reduced to something like this.

#!perl use strict; open (PASS, ">pass.rpt") || die "ERROR: cannot open"; open (FAIL, ">fail.rpt") || die "ERROR: cannot open"; open (NC, ">noCheck.rpt") || die "ERROR: cannot open"; # for test my @PASS=(); my @FAIL=(); my @NC=(); my ($ncc,$pc,$fc); # test data if required my @arr1=();# = ('A;-50','B;51','C;51','D;55'); my @arr2=();# = ('A;51' ,'B;39','C;41','F;57'); file_ext('fileA.txt',\@arr1); file_ext('fileB.txt',\@arr2); check(); print "\nTotal PASS match: $pc\n\n"; print "$_\n" for @PASS; print "\nTotal FAIL match: $fc\n\n"; print "$_\n" for @FAIL; print "\nTotal NC match: $ncc\n\n"; print "$_\n" for @NC; sub file_ext { my ($file,$ar) = @_; open (FILE,'<',$file) or die "Could not open $file"; while (<FILE>){ chomp; # name = first field, score = last field my @f = split /\s+/; #print "$f[0] $f[-1]\n"; push @$ar,"$f[0];$f[-1]"; } } sub check { foreach my $data1 (@arr1) { my ($ep1,$s1) = split ';',$data1; foreach my $data2 (@arr2) { my ($ep2,$s2) = split ';',$data2; next if ($ep1 ne $ep2); # negatives in fileA score if ($s1 =~ /-/){ $ncc++; my $line = sprintf "%-20s %4d %4d",$ep1,$s1,$s2; print NC $line."\n"; push @NC,$line; } # pass elsif (($s1 > 50) && ($s2 >40)){ $pc++; my $line = sprintf "%-20s %4d %4d",$ep1,$s1,$s2; print PASS $line."\n"; push @PASS,$line; } #fail else { $fc++; my $line = sprintf "%-20s %4d %4d",$ep1,$s1,$s2; print FAIL $line."\n"; push @FAIL,$line; } } } } =head1 fileA.txt Jane_let [0] (sa) 58.78 r 66.15 0.00 -33 Alfert_pipe (sa) 74.72 r 92.72 0.00 82 Olive_pipe[8] (sa) 64.28 f 25.40 0.00 58 mass/excel/i60 86.21 r 59.90 0.00 68 Anne_let (sa) 51.98 f 12.69 0.00 -39 yuki/099/pipe 76.52 r 94.32 0.00 -82 frey/let/sa/y589 47.79 f 99.00 0.00 78 alan/excel/sa/y589 97.00 f 96.00 0.00 -70 =head1 fileB.txt Ash_let[9] (sa) 58.78 r 66.15 0.00 33 Alfert_pipe (sa) 74.72 r 92.72 0.00 57 Olive_pipe[8] (sa) 64.28 f 25.40 0.00 20 mass/excel/i60 86.21 r 59.90 0.00 16 Sam_let (sa) 51.98 f 12.69 0.00 -39 yuki/099/pipe 76.52 r 94.32 0.00 82 frey/let/sa/y589 47.79 f 99.00 0.00 30 alan/excel/sa/y589 67.00 f 96.00 0.00 -90 =cut
If the run time is too slow then consider a hash based solution rather than using arrays.
poj


Comment on Re: Perl: How to perfectly match specific data between two files and do comparison?
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (17)
As of 2015-07-01 16:18 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 (9 votes), past polls