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

You need to change the regex that pulls the name out of the text, to something like:
/^\s*(\S+(?:.*\(.*\))?).+\s(\S+)\s*$/
Okay, here's a slightly updated version that includes that regex, and a minor cleanup:
use strict; use warnings; sub readReport { my $filename = shift; my %hash; open (my $file, '<', $filename) or die $!; while (<$file>) { next unless /^--------/ .. eof; # Bypass header if (my ($name, $score) = /^\s*(\S+(?:.*\(.*\))?).+\s(\S+)\s*$/ +) { $hash{$name} = $score; } } return \%hash; } my $reportA = readReport('filea'); my $reportB = readReport('fileb'); my %reports = ( fail => [], ignored => [], noCheck => [], pass => [] ) +; for my $name (sort keys $reportA) { my ($a,$b) = ($reportA->{$name}, $reportB->{$name}); if ($a < 0) { push $reports{'noCheck'}, "$name, $a $b" if defined $b; push $reports{'ignored'}, "$name, $a 0" unless defined $b; } elsif ($a > 50 and $b > 40) { push $reports{'pass'}, "$name, $a $b"; } else { push $reports{'fail'}, "$name, $a $b"; } } for my $name (keys %reports) { print "\nReport $name\n----------------------\n" ; print "$_\n" for @{$reports{$name}}; }

Replies are listed 'Best First'.
Re^4: Perl: How to perfectly match specific data between two files and do comparison?
by WWq (Novice) on Jul 24, 2013 at 14:35 UTC
    Thank you. It almost works! However, It prints the same scores in reports. Maybe some problem in defining $a and $b or push array. I tried to print out $a and $b after: my ($a,$b) = ($reportA->{$name}, $reportB->{$name}); The output scores are the same from file B. Perhaps I miss out something. What's wrong with that?
      It's quite suspicious that the scores are the same -- double check that you did in fact use files that have differing values for the inputs. Also rerun the script against the data from your original post.
Re^4: Perl: How to perfectly match specific data between two files and do comparison?
by WWq (Novice) on Jul 25, 2013 at 03:04 UTC
    Thank you so much Loops! It works after debugging. =)
      You're welcome. It would be very helpful if you would please make clear if something is wrong in the script as shown here, and the fix required. Others may find this thread in the future and it will be unfortunate to leave a lurking problem. Cheers.