ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> perl-win32-users
perl-win32-users
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

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved