Hi monks!,
Last week, I need a chunk of code that
autoincrement a string magically, as '++'; but not holding the first character.
So "sa9876" would result "sa9877",
"sa9999" would result "sb9999"
and "sz9999" would result "saa0000"
At first, I wrote this code:
my($f,@r)=split//,$_;
my$l=join"",@r;
++$l;
$_=$f.$l;
print $_;
So I
"autoincrement in my way" $_
Is the code too long?
I think it is, I could make it shorter...
Is this a golf?
Yes, it is...
This is second attempt
#23456789#123456789#1234567
($_,$b)=/(.)(.*)$/;$_.=++$b
Note:I would like not to use variables as
$b, cause it would be predeclared if
use strict is used, but I can't autoincrement
$DIGIT (there are read-only)
Who plays the hole?
UPDATE:
danger, great!, I didn't think in substr!
UPDATE:
Thanks
crazyinsomniac, I didn't know that
$a and
$b doesn't need to be predeclared.
I've played with
sort and is magical too. It use
$a and
$b but restore them at the end.
Update!anarion's code:
Brackets can be pulled...
++substr$_,1
Example:
perl -le '$_=sa9999;++substr$_,1;print'
Hopes
$_=$,=q,\,@4O,,s,^$,$\,,s,s,^,b9,s,
$_^=q,$\^-]!,,print
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.