SPUG: auto-increment mixed alpha-numeric

Richard Wood wildwood_players at yahoo.com
Thu Mar 13 13:28:08 CST 2003


Here is what I am using in the meantime while waiting
for an elegant solution.

#!/usr/bin/perl -w
@strings = qw(AAA AZ8 A8A A98 118 18A 1A8 8AA);
foreach $str (@strings) {
    for ($i=0;$i<3;$i++) {
	print "$str\t";
	myinc();
    }
    print "\n";
}
sub myinc {
    if ($str =~
/\d{3}|[A-Z]{3}|[A-Z]{1}\d{2}|[A-Z]{2}\d{1}/) {
	$str++;
    }
    else {
	$str =~ /(.)(.)(.)/;
	$a = $1;
	$b = $2;
	$c = $3;
	$c++;
	if (($c ne '10') && ($c ne 'AA')) {
	    $str = "$a$b$c";
	}
	else {
	    $c =~ s/.(.)/$1/;
	    $b++;
	    if (($b ne '10') && ($b ne 'AA')) {
		$str = "$a$b$c";
	    }
	    else {
		$b =~ s/.(.)/$1/;
		$a++;
		$str = "$a$b$c";
	    }
	}
    }
}

Rich Wood

--- Richard Wood <wildwood_players at yahoo.com> wrote:
> I have the need/desire to auto-increment a three
> character scalar that may contain: all numerics, all
> alphas, or mixed alpha-numeric.
> 
> auto-increment works fine for all alpha and all
> numeric and in some cases with a mix of
> alpha-numerics
> but not when a numeric precedes an alpha.  In those
> cases I get two types of results.  (e.g. '1AA'
> becomes
> 2, 'A1A' becomes 1, then 2).
> 
> Anyone have a simple solution to this?  I will
> continue to look around and try things but I thought
> I
> would ask the community before any more time slipped
> away.
> 
> Here is my test program and results:
> 
> #!/usr/bin/perl -w
> @strings = qw(AAA AA8 A8A A98 118 18A 1A8 8AA);
> foreach $str (@strings) {
>     for ($i=0;$i<3;$i++) {
> 	print "$str\t";
> 	$str++;
>     }
>     print "\n";
> }
> __END__
> AAA     AAB     AAC
> AA8     AA9     AB0
> A8A     1       2
> A98     A99     B00
> 118     119     120
> 18A     19      20
> 1A8     2       3
> 8AA     9       10
> 
> Regards,
> 
> Rich Wood
> 
> =====
> Richard O. Wood
> Wildwood IT Consultants, Inc.
> wildwood_players at yahoo.com
> 425.281.1914 mobile
> 206.544.9885 desk
> 
> __________________________________________________
> Do you Yahoo!?
> Yahoo! Web Hosting - establish your business online
> http://webhosting.yahoo.com
>
_____________________________________________________________
> Seattle Perl Users Group Mailing List  
> POST TO: spug-list at mail.pm.org
> ACCOUNT CONFIG:
> http://mail.pm.org/mailman/listinfo/spug-list
> MEETINGS: 3rd Tuesdays, U-District, Seattle WA
> WEB PAGE: www.seattleperl.org
> 


=====
Richard O. Wood
Wildwood IT Consultants, Inc.
wildwood_players at yahoo.com
425.281.1914 mobile
206.544.9885 desk

__________________________________________________
Do you Yahoo!?
Yahoo! Web Hosting - establish your business online
http://webhosting.yahoo.com



More information about the spug-list mailing list