in reply to Re^2: POE - can't increment within sub
in thread POE - can't increment within sub

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.

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

    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,

Re^4: POE - can't increment within sub
by AnomalousMonk (Archbishop) on Dec 13, 2015 at 04:11 UTC

    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:  <%-{-{-{-<

      Sorry, a numeric comparison won’t work correctly here:

      #! perl use strict; use warnings; my $sequence_number = "01"; print $sequence_number, "\n"; for (1 .. 105) { if (++$sequence_number > 99) { $sequence_number = "01"; } print $sequence_number, "\n"; }

      Output:

      14:20 >perl 1479_SoPW.pl 01 02 3 4 5 6 7 8 9 10 11 12 ... 97 98 99 01 02 3 4 5 6 7 14:20 >

      The reason is given in the documentation I quoted above:

      If you increment a variable that is numeric, or that has ever been used in a numeric context, you get a normal increment. (emphasis added)

      :-(

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