Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
There's more than one way to do things
 
PerlMonks  

Re: Comparison Of Files

by chipmunk (Parson)
on Dec 05, 2000 at 16:22 UTC ( [id://45027]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


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 05, 2000 at 19: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?)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://45027]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.