Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^2: POE - can't increment within sub

by Athanasius (Archbishop)
on Dec 12, 2015 at 04:30 UTC ( [id://1150094]=note: print w/replies, xml ) Need Help??


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

... as explained in perlop#Auto-increment-and-Auto-decrement:

The auto-increment operator has a little extra builtin magic to it. If you increment a variable that is numeric, or that has ever been used in a numeric context, you get a normal increment. If, however, the variable has been used in only string contexts since it was set, and has a value that is not the empty string and matches the pattern /^[a-zA-Z]*[0-9]*\z/, the increment is done as a string, preserving each character within its range, with carry:
print ++($foo = "99"); # prints "100" print ++($foo = "a0"); # prints "a1" print ++($foo = "Az"); # prints "Ba" print ++($foo = "zz"); # prints "aaa"

Cf. First JAPH - Spell perl in two hundred and eighty five thousand and seventy four easy steps. ;-)

Hope that helps,

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

Replies are listed 'Best First'.
Re^3: POE - can't increment within sub
by ljamison (Sexton) on Dec 12, 2015 at 04:50 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.

        For whatever reason, before I used the sprintf on the scalar, when the code would run it would consistently output 1READ instead of 00001READ

Re^3: POE - can't increment within sub
by Anonymous Monk on Dec 12, 2015 at 06:01 UTC
    I know, and that's why I always use += 1 rather than ++ in Perl :)
Re^3: POE - can't increment within sub
by ljamison (Sexton) on Dec 13, 2015 at 02:44 UTC

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

        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,

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1150094]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-03-19 11:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found