in reply to POE - can't increment within sub

Yeah, ++ and += 1 are not exactly the same.
$ perl -E 'my $x = "001"; $x += 1; say $x' 2 $ perl -E 'my $x = "001"; $x++; say $x' 002

Replies are listed 'Best First'.
Re^2: POE - can't increment within sub
by Athanasius (Archbishop) on Dec 12, 2015 at 04:30 UTC

      Unfortunately, my main problem is that when I run this example it only prints out the '00001' and ever increments it in the context I provided.

        but you have
        $stuff = $format_sequence_number . 'READ' . '00000'; $heap->{server}->put($stuff);
        And you don't change $format_sequence_number? Also, what's the point of my $format_sequence_number = sprintf("%s", $sequence_number); I don't understand what that's supposed to accomplish.
      I know, and that's why I always use += 1 rather than ++ in Perl :)

      I feel like an idiot...I didn't read over your answer carefully enough until now. I tried it with the () around the variable name and it worked correctly!!!

      Is there a way for me to use an if/else statement in this context? I want the numbers to start back at "00001" after it reaches "99999" but doing something like this:

      my $sequence_number = "00001"; if (($sequence_number)++ gt "99999") { $sequence_number = "00001"; } else { ($sequence_number)++; }

      Doesn't appear to work.

        Parentheses aren’t needed for auto-increment. In the documentation example:

        print ++($foo = "99"); # prints "100"

        the parentheses are there only to ensure that the assignment to $foo occurs before the auto-increment.

        But note that the auto-increment used here is in the prefix position. When you use an auto-increment in the postfix position, the increment occurs after the rest of the statement is executed. So when:

        if ($sequence_number++ gt "99999") {

        has been evaluated, the value in $sequence_number is one greater than the value it had in the comparison.

        A bigger problem arises from the use of gt, which makes an alphabetic comparison. So the comparison "100000" gt "99999" will actually fail, because "1" is alphabetically “less than” "9". In a case like this, it’s better to use eq for an exact comparison.

        Another thing to watch out for is that in your proposed code:

        if (($sequence_number)++ ...) { $sequence_number = "00001"; } else { ($sequence_number)++; }

        when the if clause fails, $sequence_number will be incremented twice, once in the if and again in the else. That’s not what you want.

        I gather that what you do want is a cycle of 5-digit numbers beginning with "00001", incrementing by one up to "99999", then reverting to "00001" and repeating as before. In other words, the number "00000" never appears. This can be done as follows:

        my $sequence_number = "00001"; use_seq_no($sequence_number); while (...) { if (++$sequence_number eq "99999") { $sequence_number = "00001"; } use_seq_no($sequence_number); } sub use_seq_no { ... }

        Hope that helps,

        Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

        gt (see Relational Operators in perlop) is a string (lexicographic/asciibetic) comparison, and '100000' or even '1000000000' is lexicographically less than '99999' or even '9'. Use a numeric comparison:

        c:\@Work\Perl\monks>perl -wMstrict -le "my $s = '99999'; print qq{A: '$s'}; ;; $s++; print qq{B: '$s'}; ;; $s = '00001' if $s > 99999; print qq{C: '$s'}; " A: '99999' B: '100000' C: '00001'

        Update: Oops... See Athanasius below. But this works:

        c:\@Work\Perl\monks>perl -wMstrict -le "my $s = '01'; ;; for (0 .. 105) { print qq{'$s'}; $s++; $s = '01' if do { (my $t = $s) > 99 }; } " '01' '02' '03' '04' '05' '06' ... '97' '98' '99' '01' '02' '03' '04' '05' '06' '07'


        Give a man a fish:  <%-{-{-{-<