SitePoint Sponsor

User Tag List

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

    PERL: Strange outcome of variable value manipulation

    Hi,

    I only dip my toe into PERL programming on the odd ocassion so I was wondering if anyone had any ideas as to why the below is happening:

    When I run my PERL script the variable values seem to get mixed up.

    Code:
    my $fileName = basename($maxFile,".TXT");
    my $currentSeqNum = substr($fileName,-1,1);
    my $nextSeqNum = ($currentSeqNum)++;
    
    print "File Name: $fileName\n\n";
    print "Current Sequence Num: $currentSeqNum\n\n";
    print "Next Sequence Num: $nextSeqNum\n\n";
    Which produces:

    Code:
    MaxFile: FOT05172.TXT
    
    File Name: FOT05172
    
    Current Sequence Num: 3
    
    Next Sequence Num: 2
    $maxFile gets it value from an sql query using DBI which is correct. The problem lies in the fact that the "Current Sequence Num:" and "Next Sequence Num:" values seem to be the wrong way round.

    Can anyone spot a mistake in my code?

    Thanks

    Chris

  2. #2
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,311
    Mentioned
    19 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by chris01010 View Post
    my $nextSeqNum = ($currentSeqNum)++;
    The auto-increment operator will return the value of the variable, then increment the variable. So first $nextSeqNum is assigned the value of $currentSeqNum, then $currentSeqNum is incremented. What you really wanted to do is:

    my $nextSeqNum = $currentSeqNum + 1;
    "First make it work. Then make it better."

  3. #3
    SitePoint Member
    Join Date
    Jun 2013
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, good spot.


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
  •