SPUG: auto-increment mixed alpha-numeric

David Dyck david.dyck at fluke.com
Thu Mar 13 14:37:35 CST 2003


On Thu, 13 Mar 2003 at 12:04 -0800, Chris Wilkes <cwilkes-spug at ladro.com> wrote:

> What I've done is created a new package variable called FunkyNumber that
> does your adding up in base 36 (base 10 + 26 letters).

The magic string increment is case sensitive, so you may
need to deal with lower case letters also.

> Run it and you'll see a case like this:
>   2ZY     2ZZ     300     301
> which I think is what you want.

Or is 3AA  desired from the increment of the ZZ

AAA	AAB	AAC
AA8	AA9	AB0
A8A	A8B	A8C
A98	A99	B00
118	119	120
18A	18B	18C
1A8	1A9	1B0
8AA	8AB	8AC
A9Y	A9Z	B0A
1Z8	1Z9	2A0
2ZY	2ZZ	3AA

#!/usr/bin/perl -w
@strings = qw(AAA AA8 A8A A98 118 18A 1A8 8AA A9Y 1Z8 2ZY);
foreach $str (@strings) {
    for ($i=0;$i<3;$i++) {
        print "$str\t";
	my $carry =1;
	my $right = '';
	while ($carry) {
	    if ($str =~ /^(.*?)([a-zA-Z]*[0-9]*)\z/) {
		my $match = $2;
		++$match;
		if ($carry = (length($match) > length($2))) {
		    $right = substr($match,1).$right;
		    $str = $1;
		} else {
		    $str = "$1$match";
		}
	    } else {
		++$str;
	    }
	}
	$str .= $right;
    }
    print "\n";
}

__END__

    ++  "++" behaves as the other operators above, except that if it is a
        string matching the format "/^[a-zA-Z]*[0-9]*\z/" the string increment
        described in perlop is used.


from perlop

If, however, the variable has been used
       in only string contexts since it was set, and has a value
       that is not the empty string and matches the pattern
       "/^[a-zA-Z]*[0-9]*\z/", 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'






More information about the spug-list mailing list