Gossamer Forum
Home : General : Perl Programming :

compare two text files

Quote Reply
compare two text files
list.txt (contains 3 enteries)
######
file1.html
file3.html
file2.html
######

data.txt (contains 2 enteries)
######
file3.html
file1.html
######


open (LIST, "list.txt");
@list = <LIST>;
chomp @list;
open (DATA, "data.txt");
@data = <DATA>;
chomp @data;
foreach $file (@list) {
foreach $line (@data) {
if ($file eq $line) {
print "$file exists\n";
}
}
}

############
It gives output:
file1.html exists
file3.html exists

My question how to Add file2.html DOESN't exist in the output.

The script is matching the files and reporting the files which exists, how can I get the files which are not matching?

TIA,

Zeshan.
Quote Reply
Re: [zeshan] compare two text files In reply to
Mmmm....how about something like;

Code:
# grab the data first. Stick it into a while
# loop, so we don't slup too much memory up...
open (LIST, "list.txt") || die "Cannot open list.txt. Reason: $!";
while (<LIST>) {

# now lets open the data.txt file, and we can
# put this one into an array...
open (DATA, "data.txt") || die "Cannot open file. Reason: $!";
@data = <DATA>;
close(DATA);

# set to 0, so we have a false value to start with..
$found = 0;

# go through each line in the array. If we have a
# match, skip to the end of the 'foreach', and
# define $found as 1...
foreach $line (@data) {

chomp $line;
if ($line eq $_) { $found = 1; last; }
$found = 0; # set to 0, cos it won't have been
# found if we get this far...

}

if ($found) {
print "Entry \"$_\" was found in data.txt <BR>";
} else {
print "Entry \"$_\" was NOT found in data.txt <BR>";
}

}

This is untested...so its not guarantteed to work. I'm hoping it will just give you the idea.

Cheers

Andy (mod)
andy@ultranerds.co.uk


IMPORTANT: I've now moved to ultranerds.co.uk, and the .com will no longer work!
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package (plugins total "value" $3,325 & rising, for just $350)| GLinks ULTRA Package PRO (plugins total "value" $5,625 & rising, for just $500)
Support Forum | Links SQL Plugins | DMOZ Dumps | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Compare our different Plugin packages *new* Free CSS Templates
Quote Reply
Re: [Andy] compare two text files In reply to
Ewwwwwwwwwwwww

Why are you opening the second file on *every* iteration?

You only need to open it once to compare it to the other file.
Quote Reply
Re: [Paul] compare two text files In reply to
Mmm...goood point Tongue

Something like this then;

Code:
# now lets open the data.txt file, and we can
# put this one into an array...
open (DATA, "data.txt") || die "Cannot open file. Reason: $!";
@data = <DATA>;
close(DATA);

# grab the data first. Stick it into a while
# loop, so we don't slup too much memory up...
open (LIST, "list.txt") || die "Cannot open list.txt. Reason: $!";
while (<LIST>) {

# set to 0, so we have a false value to start with..
$found = 0;

# go through each line in the array. If we have a
# match, skip to the end of the 'foreach', and
# define $found as 1...
foreach $line (@data) {

chomp $line;
if ($line eq $_) { $found = 1; last; }
$found = 0; # set to 0, cos it won't have been
# found if we get this far...

}

if ($found) {
print "Entry \"$_\" was found in data.txt <BR>";
} else {
print "Entry \"$_\" was NOT found in data.txt <BR>";
}

}

Cheers

Andy (mod)
andy@ultranerds.co.uk


IMPORTANT: I've now moved to ultranerds.co.uk, and the .com will no longer work!
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package (plugins total "value" $3,325 & rising, for just $350)| GLinks ULTRA Package PRO (plugins total "value" $5,625 & rising, for just $500)
Support Forum | Links SQL Plugins | DMOZ Dumps | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Compare our different Plugin packages *new* Free CSS Templates
Quote Reply
Re: [Andy] compare two text files In reply to
Not Working for the above given 2 text files (see the top lines), its producing the output...

Entry "file1.html " was NOT found in data.txt
Entry "file3.html " was NOT found in data.txt
Entry "file2.html " was NOT found in data.txt

though, file1.html and file2.html exists in data.txt

any help?

######################

open (DATA, "data.txt") || die "Cannot open file. Reason: $!";
@data = <DATA>;
close(DATA);

open (LIST, "list.txt") || die "Cannot open list.txt. Reason: $!";
while (<LIST>) {
$found = 0;

foreach $line (@data) {
chomp $line;
if ($line eq $_) { $found = 1; last; }
$found = 0;
}

if ($found) {
print "Entry \"$_\" was found in data.txt <BR>";
}
else
{
print "Entry \"$_\" was NOT found in data.txt <BR>";
}
}

#####################

Thanks,

Zeshan.
Quote Reply
Re: [zeshan] compare two text files In reply to
Please attach all 3 files (the two .txt file, and the .cgi). I'll have a play around with it, and see what it does.

Cheers

Andy (mod)
andy@ultranerds.co.uk


IMPORTANT: I've now moved to ultranerds.co.uk, and the .com will no longer work!
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package (plugins total "value" $3,325 & rising, for just $350)| GLinks ULTRA Package PRO (plugins total "value" $5,625 & rising, for just $500)
Support Forum | Links SQL Plugins | DMOZ Dumps | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Compare our different Plugin packages *new* Free CSS Templates
Quote Reply
Re: [Andy] compare two text files In reply to
You were chomping the list.txt but not the data.txt, due to which it was giving error.

I fixed it.

Once again I appreciate your help.

Zeshan.