in reply to Perl: How to perfectly match specific data between two files and do comparison?
Output:#!/usr/bin/perl 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 @scores = ($reportA->{$name}, $reportB->{$name}); my $rpt = "fail"; if ($scores[0] < 0) { $rpt = defined $scores[1] ? 'noCheck' : 'ignored'; $scores[1] //= 0; } elsif ($scores[0] > 50 and $scores[1] > 40) { $rpt = 'pass'; } push $reports{$rpt}, "$name, @scores"; } for my $name (keys %reports) { print "\nReport $name\n----------------------\n" ; print "$_\n" for @{$reports{$name}}; }
Report pass ---------------------- Alfert_pipe, 82 57 Report noCheck ---------------------- alan/excel/sa/y589, -70 -90 yuki/099/pipe, -82 82 Report fail ---------------------- Olive_pipe, 58 20 frey/let/sa/y589, 78 30 mass/excel/i60, 68 16 Report ignored ---------------------- Anne_let, -39 0 Jane_let, -33 0
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Perl: How to perfectly match specific data between two files and do comparison?
by WWq (Novice) on Jul 24, 2013 at 08:19 UTC | |
by Loops (Curate) on Jul 24, 2013 at 08:55 UTC | |
by WWq (Novice) on Jul 24, 2013 at 14:35 UTC | |
by Loops (Curate) on Jul 24, 2013 at 18:00 UTC | |
by WWq (Novice) on Jul 25, 2013 at 03:04 UTC | |
by Loops (Curate) on Jul 25, 2013 at 03:22 UTC |