in reply to Comparison Of Files

Here's a solution that reports the changes in both directions, using a hash for each file. It has been tested.
sub compare_sites { my($old, $new) = @_; if (-M $new > -M $old) { ($old, $new) = ($new, $old); } open(OLD, $old) or die "Can't open $old: $!\n"; open(NEW, $new) or die "Can't open $new: $!\n"; my(%old, %new); while (<OLD>) { chomp; $old{$_} = 1; } while (<NEW>) { chomp; $new{$_} = 1; } close(OLD); close(NEW); my @old = keys %old; delete @old{keys %new}; delete @new{@old}; for (sort keys %old) { print "$_ went down overnight.\n"; } for (sort keys %new) { print "$_ came up overnight.\n"; } }

Replies are listed 'Best First'.
Re: (lemming) Comparison Of Files
by lemming (Priest) on Dec 06, 2000 at 00:41 UTC

    Ok. I stole a lot of the chipmunk's code (because I'm lazy and didn't want to type...). Instead, I trade speed for memory by using one hash for both old & new.

    sub compare_sites { my($old, $new) = @_; if (-M $new > -M $old) { ($old, $new) = ($new, $old); } open(OLD, $old) or die "Can't open $old: $!\n"; open(NEW, $new) or die "Can't open $new: $!\n"; my %hash; my $omsk = 1; my $nmsk = 2; while (<OLD>) { chomp; $hash{$_} |= $omsk; } while (<NEW>) { chomp; $hash{$_} |= $nmsk; } close(OLD); close(NEW); for (sort keys %hash) { if ($hash{$_} == 1) { print "$_ went down overnight.\n"; } } for (sort keys %hash) { if ($hash{$_} == 2) { print "$_ went up overnight.\n"; } } } compare_sites("newsite1.txt", "newsite2.txt");
    Now you could just go through the hash once and push into arrays as well, but then we might as well use the chipmunk hash. (Dance or Dish?)