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

Re: Comparing Two Files

by walkingthecow (Friar)
on Jun 30, 2010 at 16:18 UTC ( #847359=note: print w/ replies, xml ) Need Help??


in reply to Comparing Two Files

Just want to thank you all for your help! I got it working, and the code I used is below. Unfortunately, I don't think it is the best way to do it, but it does work.

#!/usr/bin/perl use strict; use warnings; use Getopt::Long; use Pod::Usage; my %alias_hash; my %host_hash; my %host_contents; my %seen; my $host_id; my $file1; my $file2; GetOptions( 'h|help' => sub { pod2usage( { -verbose => 1, -input = +> \*DATA, } ); exit; }, 'm|man' => sub { pod2usage( { -verbose => 2, -input = +> \*DATA, } ); exit; }, 'f1|file1=s' => \$file1, 'f2|file2=s' => \$file2, ); pod2usage( -verbose => 1 ) unless $file1 and $file2; open(my $file1_handle, '<', $file1) or die "Could not open $file1 ($!) +\n"; while (my $line=<$file1_handle>) { chomp $line; if ($line =~ /host id="(.*?)"/) { $host_id = $1; } if ($line =~ m{<host-alias>(www.*?)</host-alias>}) { $alias_hash{$host_id}{$1} = -1; } if (!$seen{$host_id}) { $host_hash{$host_id} = -1; } $seen{$host_id} = 1; } close $file1_handle; %seen=(); open(my $file2_handle, '<', $file2) or die "Could not open $file2 ($!) +\n"; while (my $line=<$file2_handle>) { chomp $line; if ($line =~ /host id="(.*?)"/) { $host_id = $1; } if ($line =~ m{<host-alias>(www.*?)</host-alias>}) { $alias_hash{$host_id}{$1}++; } if (!$seen{$host_id}) { $host_hash{$host_id}++; } $seen{$host_id} = 1; } close $file1_handle; for my $k1 ( keys %alias_hash ) { for my $k2 ( keys %{ $alias_hash{$k1} } ) { print "$k2 exists in only $file1\n" if $alias_hash{$k1}{$k2} +== -1; print "$k2 exists in only $file2\n" if $alias_hash{$k1}{$k2} +== 1; } } while ( my ($key, $value) = each(%host_hash) ) { print "$key exists in only $file1\n" if $host_hash{$key} == -1; print "$key exists in only $file2\n" if $host_hash{$key} == 1; }
P.S.: You may notice that I use Pod::Usage and have not put anything in there for DATA. I just haven't gotten around to it yet, but it doesn't impact this script from working in any way ;)


Comment on Re: Comparing Two Files
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (14)
As of 2014-04-16 16:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (432 votes), past polls