467,179 Members | 1,214 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,179 developers. It's quick & easy.

what's wrong with my loop?

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
  • viewed: 1094
Share:
6 Replies
KevinADC
Expert 2GB
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
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
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
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 2GB
I don't know what the problem is or if there is another module you can use.
Jul 6 '08 #6
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.

Similar topics

226 posts views Thread by Stephen C. Waterbury | last post: by
51 posts views Thread by WindAndWaves | last post: by
6 posts views Thread by Niklaus | last post: by
669 posts views Thread by Xah Lee | last post: by
30 posts views Thread by Bill Reid | last post: by
16 posts views Thread by chutsu@gmail.com | last post: by
10 posts views Thread by DavidSeck.com@gmail.com | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.