By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,776 Members | 1,777 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,776 IT Pros & Developers. It's quick & easy.

what's wrong with my loop?

P: 72
i wish to compare 1 file with multiple files (in a folder)
@allfile - >> the paths for all files in a folder

but once i run the code below, it keep loop at print "comparing $masterfile with $file1";
if i delete the compare function it works well.
what's wrong? Thanks


Expand|Select|Wrap|Line Numbers
  1. use File::Compare;
  2. foreach $file (@allfile) {
  3. my $file1=$file;
  4. $masterfile='c:\\ts\\compare1.pl';
  5.             print "comparing $masterfile with $file1";
  6.     if (compare("$masterfile","$file1") == 0) {
  7.         print "They're equal\n";
  8.     } else {
  9.          print "They're not equal\n";
  10.              }
  11.  
  12. }
Jul 5 '08 #1
Share this Question
Share on Google+
6 Replies


KevinADC
Expert 2.5K+
P: 4,059
i wish to compare 1 file with multiple files (in a folder)
@allfile - >> the paths for all files in a folder

but once i run the code below, it keep loop at print "comparing $masterfile with $file1";
if i delete the compare function it works well.
what's wrong? Thanks


Expand|Select|Wrap|Line Numbers
  1. use File::Compare;
  2. foreach $file (@allfile) {
  3. my $file1=$file;
  4. $masterfile='c:\\ts\\compare1.pl';
  5.             print "comparing $masterfile with $file1";
  6.     if (compare("$masterfile","$file1") == 0) {
  7.         print "They're equal\n";
  8.     } else {
  9.          print "They're not equal\n";
  10.              }
  11.  
  12. }
Here is the code a bit cleaned up. It looks like it should work OK.

Expand|Select|Wrap|Line Numbers
  1. use File::Compare;
  2. my $masterfile = 'c:/ts/compare1.pl'; # <-- forward slashes OK with windows
  3. foreach my $file (@allfile) {
  4.    print "comparing $masterfile with $file\n";
  5.    if (compare($masterfile, $file) == 0) {
  6.       print "They're equal\n";
  7.    }
  8.    else {
  9.       print "They're not equal\n";
  10.    }
  11. }
run the code and post what this line prints:

Expand|Select|Wrap|Line Numbers
  1.  print "comparing $masterfile with $file";
Jul 5 '08 #2

P: 72
It's still the same where it's pause at the code of compare.
comparing c:/ts/compare1.pl with c:\ts\aaa.pl

Once i remove the compare function, it's become ok.

should be somethingwrong with the compare function.

i noticed that whenever i use
Expand|Select|Wrap|Line Numbers
  1. print Dumper "comparing $masterfile with $file\n";
i can print the path correctly meaning got double '\\'

while if i use the
Expand|Select|Wrap|Line Numbers
  1. print "comparing $masterfile with $file\n";
it all change to single '\'
Jul 6 '08 #3

P: 72
comparing c:/ts/compare1.pl with c:\ts\aaa.pl

i tried to change the "\" to "/"... but the code stil failed (keep looping when comapring the first file ) at the compare function.

help. thanks
Jul 6 '08 #4

P: 72
i check with open(file, $file)|| or die $;
it's working fine. meaning there's no broken link for the path.
beside using compare function, is there any module tat i can use?
Please guide.
Jul 6 '08 #5

KevinADC
Expert 2.5K+
P: 4,059
I don't know what the problem is or if there is another module you can use.
Jul 6 '08 #6

P: 72
It's ok. thanks for your time.
Jul 7 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.