RE: Comparing 2 text files
by Peter Eisengrein other posts by this author
Nov 11 2005 12:48PM messages near this date
view in the new Beta List Site
Comparing 2 text files
|
Re: Comparing 2 text files
> What is the best way to compare 2 text files for
> matching. Let's say both files have just people names
> Thanks
>
Does it matter what position they are in, or do you only want to know what
exists in both files? If it doesn't matter, you can do it as follows:
my $count = 0;
my $count2 = 0;
my $line;
my %hash;
open(FILE1, $file1) || die "Can't read $file1 : $!\n";
foreach $line (<FILE1> )
{
$count++;
chomp($line);
$hash{$line}=$count;
}
close(FILE1);
open(FILE2, $file2) || die "Can't read $file2 : $!\n";
foreach $line (<FILE2> )
{
$count2++;
chomp($line);
if ($hash{$line})
{
print "$line exists in file1.\n";
}
else
{
print "$line does NOT exist in file1.\n";
}
}
### END
if you need to know positional, then you can use the value of $hash{$line}
and compare it to $count in the second pass.
> File1 File2
> James Joe
> Tina Patrick
> Steve James
> ... ....
_______________________________________________
Perl-Win32-Users mailing list
Perl-Win32-Users@[...].com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Thread:
Michael D Schleif
Michael D Schleif
Chris Wagner
$Bill Luebkert
$Bill Luebkert
Michael D Schleif
$Bill Luebkert
Michael D Schleif
$Bill Luebkert
Michael D Schleif
$Bill Luebkert
James Sluka
Ted Zeng
$Bill Luebkert
Ted Zeng
Paul
Peter Eisengrein
Trevor Joerges
Jim Guion
$Bill Luebkert
James Sluka
$Bill Luebkert
Chris Wagner
|