Chris Stinemetz wrote:
Hello list,

Hello,

I have two hashes that I would like to compare the values where the
keys are the same. If there are any discrepancies I would like to
print them. I have been struggling in find a solution. Below is what I
have thus far:

#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;

my $firstFile = "out.txt";
my $secFile = "outADD.txt";
my $deltaFile = "datalinkDeltas.txt";

open my $firstFH,'<',$firstFile or die "ERROR opening $firstFile: $!";
open my $secFH,'<',$secFile or die "ERROR opening $secFile: $!";
open my $deltaFH,'>',$deltaFile or die "ERROR opening $deltaFile: $!";

my %firstFile;
my %secondFile;

while (my $line =<$firstFH>) {
        my ($k,$v) = split(/\s/,$line);
        $firstFile{$k} = $v;
}

while (my $line =<$secFH>) {
        my ($k,$v) = split(/\s/,$line);
        $secondFile{$k} = $v;
}
print Dumper (\%firstFile,\%secondFile);


Do you really need two hashes?  Perhaps you want something like this:

my %firstFile;

while ( <$firstFH> ) {
        my ( $k, $v ) = split;
        $firstFile{ $k } = $v;
}

while ( <$secFH> ) {
        my ( $k, $v ) = split;
        if ( exists $firstFile{ $k } && $firstFile{ $k } ne $v ) {
                print "$k: $firstFile{ $k } ne $v\n";
        }
}




John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.                   -- Albert Einstein

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to