SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Jun 2013
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    If statement not working

    Hi,

    I'm working with the below if statement in my code and it doesn't seem to matching the condition correctly:

    Code:
    if ($maxFile ne "") { 
                         my $fileName = basename($maxFile,".TXT");
                         my $currentSeqNum = substr($fileName,-1,1);
                         my $nextSeqNum = $currentSeqNum + 1;
                        }
       else { my $nextSeqNum = 0; }
    
    print "File Name: $fileName\n\n";
    print "Current Sequence Num: $currentSeqNum\n\n";
    print "Next Sequence Num: $nextSeqNum\n\n";
    
    }
    this returns:

    Code:
    MaxFile: FOT05172.TXT
    
    File Name: 
    
    Current Sequence Num: 
    
    Next Sequence Num:
    $maxfile is returned from a DBI call.

    Any ideas where I'm going wrong?

    Thanks

    Chris

  2. #2
    Just Blow It bronze trophy
    DaveMaxwell's Avatar
    Join Date
    Nov 1999
    Location
    Mechanicsburg, PA
    Posts
    7,264
    Mentioned
    115 Post(s)
    Tagged
    1 Thread(s)
    I'm not a perl guy, but this is a typical problem - you're variables are out of scope. I'm surprised you're not getting an error on the print statements, but the print processor must be able to handle them OK.

    The reason you get no values is because you're defining them inside the if statement, which means they only exist within the if statement itself. You need to define them outside the if statement, then remove the my from within the if statement (otherwise, it'll create ANOTHER version of the variables with a different scope).

    See this page where I verified the answer before I posted: http://www.expertwebinstalls.com/cgi...variables.html
    Dave Maxwell - Manage Your Site Team Leader
    My favorite YouTube Video! | Star Wars, Dr Suess Style
    Learn how to be ready for The Forums' Move to Discourse

  3. #3
    SitePoint Wizard Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,278
    Mentioned
    50 Post(s)
    Tagged
    2 Thread(s)
    This recently came up in the London.pm mailing list.

    http://london.pm.org/pipermail/londo...7/subject.html
    Start with the first mail by Andrew Beverley, who did the same thing as you. In the languages I write I generally also declare (undefined) variables outside and set new values inside the condition. You only need to do this if these variables will be referenced after the condition, as you are doing, but if you were doing something like

    if (something) {
    my $foo = bar;
    run a sub with $foo and return, never to call $foo again;
    }
    else (other stuff) {
    my @bar = baz, quux...;
    do something else and never call @bar again;
    }

    Perl will let you try to print undefined variables because it assumes you know what you're doing, but while developing you might want to let the interpreter check undefined/undeclared variables using the -w or "use warnings" options. This is also nice for when you declare a variable properly and later reference it, but with a misspelling. When it works and when it ignores, so watch out for that "my $nextSeqNum = $currentSeqNum + 1;" thing.

    Also, you are comparing a file(handle) to empty string? You might rather want to use -x to test either that your file exists, or that it's not empty, whichever is more important to you (or you can test for both).

  4. #4
    SitePoint Member
    Join Date
    Aug 2011
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DaveMaxwell View Post
    I'm not a perl guy, but this is a typical problem - you're variables are out of scope. I'm surprised you're not getting an error on the print statements, but the print processor must be able to handle them OK.
    The lack of an error message is because the OP's code doesn't use the "strict" or "warnings" pragmas:

    Code:
    $ perl -e 'print "$currentSeqNum\n"'
    
    $ perl -e 'use warnings; print "$currentSeqNum\n"'
    Name "main::currentSeqNum" used only once: possible typo at -e line 1.
    Use of uninitialized value $currentSeqNum in concatenation (.) or string at -e line 1.
    
    $ perl -e 'use strict; print "$currentSeqNum\n"'
    Global symbol "$currentSeqNum" requires explicit package name at -e line 1.
    Execution of -e aborted due to compilation errors.
    Catching problems like this is one of the major reasons why it's standard advice to always
    Code:
    use strict;
    use warnings;
    in your Perl programs.

  5. #5
    SitePoint Member
    Join Date
    Jun 2013
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks all. Very informative and extremely useful.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •