Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

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

by AnomalousMonk (Archbishop)
on Dec 13, 2015 at 04:11 UTC ( [id://1150147]=note: print w/replies, xml ) Need Help??


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

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

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

    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://1150147]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2024-04-23 23:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found