what's wrong with my loop?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MyMarlboro
    New Member
    • Mar 2008
    • 71

    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


    Code:
    use File::Compare;
    foreach $file (@allfile) {
    my $file1=$file;
    $masterfile='c:\\ts\\compare1.pl';
                print "comparing $masterfile with $file1";
    	if (compare("$masterfile","$file1") == 0) {
    		print "They're equal\n";
    	} else {
    		 print "They're not equal\n";
    	         }
    	
    }
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    Originally posted by MyMarlboro
    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


    Code:
    use File::Compare;
    foreach $file (@allfile) {
    my $file1=$file;
    $masterfile='c:\\ts\\compare1.pl';
                print "comparing $masterfile with $file1";
    	if (compare("$masterfile","$file1") == 0) {
    		print "They're equal\n";
    	} else {
    		 print "They're not equal\n";
    	         }
    	
    }
    Here is the code a bit cleaned up. It looks like it should work OK.

    Code:
    use File::Compare;
    my $masterfile = 'c:/ts/compare1.pl'; # <-- forward slashes OK with windows
    foreach my $file (@allfile) {
       print "comparing $masterfile with $file\n";
       if (compare($masterfile, $file) == 0) {
          print "They're equal\n";
       }
       else {
          print "They're not equal\n";
       }
    }
    run the code and post what this line prints:

    Code:
     print "comparing $masterfile with $file";

    Comment

    • MyMarlboro
      New Member
      • Mar 2008
      • 71

      #3
      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
      Code:
      print Dumper "comparing $masterfile with $file\n";
      i can print the path correctly meaning got double '\\'

      while if i use the
      Code:
      print "comparing $masterfile with $file\n";
      it all change to single '\'

      Comment

      • MyMarlboro
        New Member
        • Mar 2008
        • 71

        #4
        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

        Comment

        • MyMarlboro
          New Member
          • Mar 2008
          • 71

          #5
          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.

          Comment

          • KevinADC
            Recognized Expert Specialist
            • Jan 2007
            • 4092

            #6
            I don't know what the problem is or if there is another module you can use.

            Comment

            • MyMarlboro
              New Member
              • Mar 2008
              • 71

              #7
              It's ok. thanks for your time.

              Comment

              Working...