This should do it...

my %all_keys = ();

foreach my $key (keys %hash1) {
  $all_keys{$key} = 1;
}

foreach my $key (keys %hash2) {
  $all_keys{$key} = 1;
}

foreach my $page ( sort( keys(%all_keys) ) ) {
  my $diff = $hash1{$page} - $hash2{$page};
  my $diff = abs($diff); # remove "-" sign
  print "$page $diff\n";
}

It can be done a lot more compact than this, but I thought this would be a
bit more understandable.

Rob



-----Original Message-----
From: Pam Derks [mailto:[EMAIL PROTECTED]]
Sent: Friday, January 31, 2003 3:21 PM
To: [EMAIL PROTECTED]
Subject: newbie stuck in a hash


Hi all,

I have 2 files that contain a filename and # of hits
I've split the contents of these 2 files into %hash1 and %hash2

I want to find out the difference in the # of hits 

can someone shed some light on the approach I should take.

thanks in advance,
Pam


I've gotten this far:

sample data:
FILE1
arts.html 95
arttherapy.html 102
award.html 277
bayart.html 1
best.html 1


FILE2
arts.html 101
arttherapy.html 103
award.html 282
bayart.html 2
best.html 2
splat.html 10  (note this is not in FILE1)


DESIRED OUTPUT:
arts.html 6
arttherapy.html 1
award.html 10
best.html 1
splat.html 10




#!/usr/bin/perl -w

$dir = "/dept/unxmkt/bin/hits/news/by_month";

chomp(@ARGV);
$file1 = shift(@ARGV);
$file2 = shift(@ARGV);

print("$file1 $file2\n");

open(FILE1, "$dir/$file1.total") or die ("nope: $!");
open(FILE2, "$dir/$file2.total") or die ("nope: $!");


while(<FILE1>){
        my(%hash1, $key1, $value1);
        ($key1, $value1) = chomp && split;
        
        $hash1{$key1} = $value1;

        for (keys %hash1){
                print("$hash1{$key1}\n");
        }

}

print("------------------------------\n");


while(<FILE2>){
        my(%hash2, $key2, $value2);
        ($key2, $value2) = chomp && split;
        
        $hash2{$key2} = $value2;

        for (keys %hash2){
                print("$hash2{$key2}\n");
        }

}


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to