Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Whats with the autoincrementation of a string

by bingalee (Acolyte)
on Jun 14, 2013 at 18:50 UTC ( #1039016=perlquestion: print w/ replies, xml ) Need Help??
bingalee has asked for the wisdom of the Perl Monks concerning the following question:

Im trying to figure this out but I dont get it. Here's the code

#!/usr/bin/perl use warnings; $a = "A9"; print ++$a, "\n"; $a = "bz"; print ++$a, "\n"; $a = "Zz"; print ++$a, "\n"; $a = "z9"; print ++$a, "\n"; $a = "9z"; print ++$a, "\n";

OUTPUT

>perl auto2.plx B0 ca AAa aa0 10 >

I get the first two outputs, A turns to B and 9 to 0.

But how does "Zz" become "AAa", "z9" become "aa0" and so on.. *confused*

Comment on Whats with the autoincrementation of a string
Select or Download Code
Re: Whats with the autoincrementation of a string
by space_monk (Chaplain) on Jun 14, 2013 at 18:54 UTC
    Note that the incrementation sequences are
    0..9 => 0 + carry|1 i.e 10 A..Z => A + carry|A i.e. AA a..z => a + carry|a i.e aa
    bz => ca
    wrapping the z->a gives a carry to the b next in Lower a..z is c.
    Zz => AAa
    wrapping the z->a gives a carry to the Z next in Upper case A..Z is AA.
    z9 => aa0
    incrementing 9 wraps round to zero, passing a carry up to the z. Next up from z is aa
    9z => 10
    tricked you!! - string autoincrement only works on patterns matching regex /^[a-zA-Z]*[0-9]*\z/, this doesn't match that pattern, so treats leading number(s) as the (numeric?) value and increments that. 9 goes to 10 :-)

    If you spot any bugs in my solutions, it's because I've deliberately left them in as an exercise for the reader! :-)

      ok, so what about something that doest end with a "z" or a "9". Like will "zc" become "aad?"

        No, "zc" will become "zd". Similarly, 93 + 1 = 94, not 104.
        لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Whats with the autoincrementation of a string
by LanX (Canon) on Jun 14, 2013 at 18:56 UTC
    Think of every position as a wheel in a mechanical counter.

    There are three kinds of wheels 0-9, a-z and A-Z which can be independently be chosen for each position.

    Clearer now? =)

    edit
    perlop
    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'

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      Not quite independently. Any numeric wheels must be at the back.

      e.g. 'Aa00' will work, '00Aa' won't.

      If you spot any bugs in my solutions, it's because I've deliberately left them in as an exercise for the reader! :-)
        Sure, anything with leading digits which isn't a proper number is dealt by atoi in numeric context, IIRC that's due to some C legacy.

        DB<109> @a=a..c => ("a", "b", "c") DB<110> $a[" 1x"] => "b"

        plz note: only with no warnings!

        Cheers Rolf

        ( addicted to the Perl Programming Language)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1039016]
Approved by muba
Front-paged by MidLifeXis
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (11)
As of 2014-12-28 02:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (178 votes), past polls