I massaged the code a bit to work on the boundary cases, and added a reverse (autoincrement) output to test the input. Unfortunately it seems to crap out on punctuation now.

Here is the output:

--"120" gives "119" reverse "120"
--"abc" gives "abb" reverse "abc"
--"testbaA" gives "testazZ" reverse "testbaA"
--"a" gives "" reverse "1"
--"aaa" gives "zz" reverse "aaa"
--"a01" gives "a00" reverse "a01"
--"aa00" gives "z99" reverse "aa00"


Here is the code:

use strict; use warnings; sub predchar { # modify argument in place. # return true if no carry (done). my $ord= ord($_[0]); my $nocarry; # define the ranges available my @ranges= ([ord('a'),ord('z')], [ord('A'),ord('Z')] ); foreach my $range (@ranges) { my ($first,$last)= @$range; next unless $ord >= $first && $ord <= $last; # my range? if ($ord == $first) { $ord= $last; } else { $nocarry = 1; --$ord; } } $_[0]= chr($ord); return $nocarry; } sub magic_decrement($) { my $instr = $_; my $numpart = $instr; my $alphapart = $instr; $numpart =~ s/[A-Za-z]+//; $alphapart =~ s/[0-9]+//; my $numlen = length($numpart); $numpart--; if($numpart == -1) { $numpart = '9' x $numlen; my @chars= split ('', $alphapart); my @retarr = @chars[0 .. $#chars]; for (my $loop= $#chars; $loop>=0; --$loop) { last if predchar ($retarr[$loop]); } $alphapart = join ('',@retarr); $alphapart =~ s/z$// if($alphapart =~ /^z+$/); } else { $numpart = '0' . $numpart if($numlen != length($numpart)); } return $alphapart . $numpart; } sub test { my $s= shift || $_; my $result= magic_decrement($s); my $reverse = $result; $reverse++; print qq{--"$s" gives "$result" reverse "$reverse"\n}; } while (<DATA>) { chomp; test; } __DATA__ 120 abc testbaA a aaa a01 aa00

In reply to Re: Magical Auto-Decrement by Anonymous Monk
in thread Magical Auto-Decrement by John M. Dlugosz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.