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

Re: Comparison Of Files

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


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?)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://45027]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2025-01-25 07:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which URL do you most often use to access this site?












    Results (70 votes). Check out past polls.