SPUG: auto-increment mixed alpha-numeric

dleonard at dleonard.net dleonard at dleonard.net
Thu Mar 13 19:21:04 CST 2003


Felt like writing a bit of perl since I don't do much of that anymore...

sub mixed_inc {
 my $in = shift;
 my @int = reverse unpack "c" x 3, $in;

 $int[0]++;
 if ($int[0] > 90) {
  $int[0] = 48;
  $int[1]++;
  if ($int[1] > 90) {
   $int[1] = 48;
   $int[2]++;
   if ($int[2] > 90) {
    $int[2] = 48;
    $int[3] = defined $int[3] ? $int[3]++ : 49;
    $int[3] = ($int[3] > 57 && $int[3] < 65) ? 65 : $int[3];
   }
   $int[2] = ($int[2] > 57 && $int[2] < 65) ? 65 : $int[2];

  }
  $int[1] = ($int[1] > 57 && $int[1] < 65) ? 65 : $int[1];
 }
 $int[0] = ($int[0] > 57 && $int[0] < 65) ? 65 : $int[0];

 my $out = scalar @int;
 return pack "c$out", reverse @int;
} #mixed_inc

-- 

<Douglas Leonard>
<dleonard at dleonard.net>
<If I were a cat I'd be dead of old age.>

On Thu, 13 Mar 2003, Richard Wood wrote:

> Thanks for the ideas,
>
> glad to see I wasn't missing something obvious.
>
> Rich Wood
>
> --- Richard Wood <wildwood_players at yahoo.com> wrote:
> > 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
> >
> _____________________________________________________________
> > 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
> _____________________________________________________________
> 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
>




More information about the spug-list mailing list