Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: string increment

by prad_intel (Monk)
on Sep 13, 2005 at 07:19 UTC ( [id://491499]=note: print w/replies, xml ) Need Help??


in reply to string increment

Hi Corion and others,

Well I guess I didnt frame my words right in the previous , I wanted to increment the whole string and also wanted to know the behaviour of the charecters in a string being incremented from the right to left.

regards

prad

Replies are listed 'Best First'.
Re^2: string increment
by holli (Abbot) on Sep 13, 2005 at 07:32 UTC
    The behaviour is the same as if you increment a number, only the "numbers" are not 0-9, but a-z. The simplest way to see that is to start "counting" from "a".
    $s = "a"; for (1..702) { print $s++, "\n"; }
    prints
    a b c ... z aa ab ac ... az ba bb bc ... zz


    holli, /regexed monk/
Re^2: string increment
by reasonablekeith (Deacon) on Sep 13, 2005 at 07:29 UTC
    well have a look at the "perlop" documentaion (auto-increment operator) to read about the behaviour.

    as for the "increment the whole string" comment, you are doing. If you increment the number 4356 to 4357, you only change one 'character', but you are operating on the whole 'string'. This is exactly the same for your example, where only the last character has rolled over, which increments the last but one. If you want to increment each character in the string, you may want to do something like this.

    $sample='prad'; print join('', map { ++$_} split(//, $sample) ); print"$sample";
    but be careful, incrementing 'z' in this fasion would return 'aa'
    ---
    my name's not Keith, and I'm not reasonable.
Re^2: string increment
by davido (Cardinal) on Sep 13, 2005 at 07:34 UTC

    The behavior is not unusual. If you incremented 1111 you would get 1112, and if you incremented it ten times you would get 1121. Eleven times would give you 1122. A hundred times would give you 1211... you get the idea.

    So, given that alpha characters can also be incremented, it should come as no surprise that they're incremented right to left also; consider the right-most digit to be the least significant digit, whether it's a number or an alpha character. Thus, 'aaaa' would become 'aaab'. Increment it enough times, it it becomes 'aaba', 'aabb', 'aabc' ... 'aaca', 'aacb', 'aacc', and so on, until you finally arrive at 'zzzz'.

    If you really just wanted to increment each character by one, you might consider constructing a regexp like this:

    $string =~ s/(.)/$char = $1;++$char/eg;

    Dave

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2024-04-19 21:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found