Loading ...
Sorry, an error occurred while loading the content.

Problem with 'if' evaluation

Expand Messages
  • Bill Walton
    Greetings! I m having what I assume is another stupid newbie problem and would really appreciate any help understanding it. The script below is only supposed
    Message 1 of 3 , Dec 3, 2005
    • 0 Attachment
      Greetings!

      I'm having what I assume is another "stupid newbie problem" and would really appreciate any help understanding it.

      The script below is only supposed to process the XML files in a given directory.

      #!/usr/bin/perl -w
      use warnings;
      use XML::Simple;
      use File::Copy;

      $SourceDir = "d:/perl/BegPerl/test/"; # assign a directory

      opendir DH, "$SourceDir" or die "Couldn't open the $SourceDir directory: $!";

      while ($_ = readdir(DH))
      {
      $filename = $_;

      # This works. No directories are processed.
      next if (-d "$SourceDir$filename"); # skip directories

      $position = index($filename,"xml");

      # This doesn't work. The 'test.sav' file is processed below.
      next if (not $position); # skip non-XML files

      print 'string "xml" found at position ';
      print $position;
      print " in $filename\n\n";
      }

      The "d:/perl/BegPerl/test/" directory contains 3 directories and three files.

      The output of the script is:

      string "xml" found at position 6 in test1.xml

      string "xml" found at position -1 in test3.sav

      string "xml" found at position 6 in test4.xml

      I do not understand why the first 'next' statement works, but the second doesn't. The problem seems to be about the 'if' statement. If I remove the 'not', no files are processed. With it in, all files are processed.

      Can anybody help, please?

      Thanks,
      Bill

      [Non-text portions of this message have been removed]
    • Jeff 'japhy' Pinyan
      ... Did you read the documentation for the index() function? It returns -1 if the substring wasn t found in the string, NOT 0. ... You don t want next if (not
      Message 2 of 3 , Dec 3, 2005
      • 0 Attachment
        On Dec 3, Bill Walton said:

        > $position = index($filename,"xml");
        >
        > # This doesn't work. The 'test.sav' file is processed below.
        > next if (not $position); # skip non-XML files

        Did you read the documentation for the index() function? It returns -1 if
        the substring wasn't found in the string, NOT 0.

        > The output of the script is:
        >
        > string "xml" found at position 6 in test1.xml
        > string "xml" found at position -1 in test3.sav
        > string "xml" found at position 6 in test4.xml
        >
        > I do not understand why the first 'next' statement works, but the second
        >doesn't. The problem seems to be about the 'if' statement. If I remove
        >the 'not', no files are processed. With it in, all files are processed.

        You don't want

        next if (not $position);

        but rather

        next if $position == -1;

        However, this won't help you if you've got a file named 'notxml.sav'.
        What you REALLY mean to say is "make sure the last four characters are
        '.xml'", which you can say in two simple ways:

        next unless substr($filename, -4) eq '.xml';

        or

        next unless $filename =~ /\.xml$/;

        Of course, you can also use 'if not' instead of 'unless', or move the
        'not' to the operator itself:

        next if substr($filename, -4) ne '.xml';
        next if $filename !~ /\.xml$/;

        --
        Jeff "japhy" Pinyan % How can we ever be the sold short or
        RPI Acacia Brother #734 % the cheated, we who for every service
        http://www.perlmonks.org/ % have long ago been overpaid?
        http://princeton.pm.org/ % -- Meister Eckhart
      • Bill Walton
        ... From: Jeff japhy Pinyan To: perl-beginner@yahoogroups.com Sent: Saturday, December 03, 2005 2:31 PM Subject: Re: [PBML] Problem with if evaluation ...
        Message 3 of 3 , Dec 3, 2005
        • 0 Attachment
          ----- Original Message -----
          From: Jeff 'japhy' Pinyan
          To: perl-beginner@yahoogroups.com
          Sent: Saturday, December 03, 2005 2:31 PM
          Subject: Re: [PBML] Problem with 'if' evaluation


          On Dec 3, Bill Walton said:

          > $position = index($filename,"xml");
          >
          > # This doesn't work. The 'test.sav' file is processed below.
          > next if (not $position); # skip non-XML files

          Did you read the documentation for the index() function? It returns -1 if the substring wasn't found in the string, NOT 0.
          [BW] Yep. I read it. Just forgot that, in perl, '-1' was TRUE. Told you it was a 'stupid newbie' problem ;-) Sorry about that. And thanks for the reply, Jeff.

          Best regards,
          Bill

          [Non-text portions of this message have been removed]
        Your message has been successfully submitted and would be delivered to recipients shortly.