Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > alphanumeric counter - howto?

Reply
Thread Tools

alphanumeric counter - howto?

 
 
Jeff
Guest
Posts: n/a
 
      11-12-2005
Paul Lalli wrote:
> Agreed. I wish the author had included more comments in the code to
> explain his/her line of thought.


I thought it was mandatory that authors remove any useful comments from code before
publishing to CPAN. I've had my head in a few CPAN modules recently, and I can't find a
useful comment anywhere!

~Jeff
 
Reply With Quote
 
 
 
 
Dr.Ruud
Guest
Posts: n/a
 
      11-12-2005
Brian McCauley:

> Perl's builtin
> string counter mechanism will count from 000000 to 999999 or from
> AAAAAA to ZZZZZZ. (i.e. base 10 or 26).


Or from A00000 to ZZZZZZ?

BTW that also explains the substr(1,

--
Affijn, Ruud

"Gewoon is een tijger."
 
Reply With Quote
 
 
 
 
Dr.Ruud
Guest
Posts: n/a
 
      11-12-2005
Dr.Ruud:
> Brian McCauley:


>> Perl's builtin
>> string counter mechanism will count from 000000 to 999999 or from
>> AAAAAA to ZZZZZZ. (i.e. base 10 or 26).

>
> Or from A00000 to ZZZZZZ?
>
> BTW that also explains the substr(1,


Oops:

$ perl -e '$a = "A99"; $a++; print "$a\n"'
B00

(so not AA0)

--
Affijn, Ruud

"Gewoon is een tijger."
 
Reply With Quote
 
Oliver Bleckmann
Guest
Posts: n/a
 
      11-12-2005
"Dr.Ruud" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
> $ perl -e '$a = "A99"; $a++; print "$a\n"'
> B00


This ist strange, but I cannot use it for a counter...


 
Reply With Quote
 
Oliver Bleckmann
Guest
Posts: n/a
 
      11-12-2005
ok, i did it myself.
now this is my solution...

sub base36
{
my ($num) = @_;
use integer;
my @chars = ('0'..'9', 'A'..'Z');
my $result = "";
for(my $b=@chars; $num; $num/=$b)
{
$result .= $chars[$num % $b];
}
return scalar reverse $result;
}

for ($i = 0; $i <= 2000; $i++ )
{
$tmp = base36($i);
for ( $c = length($tmp); $c < 6; $c++ )
{
$tmp = "0" . "$tmp";
}
print "$tmp\n";
}


 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      11-13-2005
Oliver Bleckmann:

> sub base36
> {
> my ($num) = @_;
> use integer;
> my @chars = ('0'..'9', 'A'..'Z');
> my $result = "";
> for(my $b=@chars; $num; $num/=$b)
> {
> $result .= $chars[$num % $b];
> }
> return scalar reverse $result;
> }



Variant:

#!/usr/bin/perl
use strict; use warnings;

use constant Chars => ('0'..'9', 'A'..'Z', 'a'..'z');
use constant MaxBase => scalar Chars;

{ local ($,, $\) = ("\t", "\n");

for (my $i = 0; $i != 30; $i++) {
print $i, int2base($i, 16, 4);
}
}

sub int2base {
use integer;

my ($ret, $num, $base, $minlen) = ('', @_);
return undef if $num < 0
|| ($base ||= 10) < 2
|| MaxBase < $base
|| ($minlen ||= 1) < 1;

for (; $num; $num /= $base) {
$ret .= (Chars)[$num % $base];
}
return scalar reverse $ret . '0'x($minlen - length($ret));
}

--
Affijn, Ruud

"Gewoon is een tijger."
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      11-13-2005
"Dr.Ruud" <(E-Mail Removed)> wrote in news:dl7o6h.1h4.1
@news.isolution.nl:

> return undef if $num < 0
> || ($base ||= 10) < 2
> || MaxBase < $base
> || ($minlen ||= 1) < 1;


You should use a plain return here rather than returning undef. Otherwise,
in list context, the sub will return a non-empty list with one element,
foiling conditionals.

Sinan

--
A. Sinan Unur <(E-Mail Removed)>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html

 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      11-13-2005
A. Sinan Unur schreef:
> Dr.Ruud:


>> return undef if $num < 0
>> || ($base ||= 10) < 2
>> || MaxBase < $base
>> || ($minlen ||= 1) < 1;

>
> You should use a plain return here rather than returning undef.
> Otherwise, in list context, the sub will return a non-empty list with
> one element, foiling conditionals.


Yes, thanks. I actually had read about it not too long ago...

--
Affijn, Ruud

"Gewoon is een tijger."
 
Reply With Quote
 
Brad Baxter
Guest
Posts: n/a
 
      11-15-2005
Dr.Ruud wrote:
> #!/usr/bin/perl
> use strict; use warnings;
>
> use constant Chars => ('0'..'9', 'A'..'Z', 'a'..'z');
> use constant MaxBase => scalar Chars;
>
> { local ($,, $\) = ("\t", "\n");
>
> for (my $i = 0; $i != 30; $i++) {
> print $i, int2base($i, 16, 4);
> }
> }
>
> sub int2base {
> use integer;
>
> my ($ret, $num, $base, $minlen) = ('', @_);
> return undef if $num < 0
> || ($base ||= 10) < 2
> || MaxBase < $base
> || ($minlen ||= 1) < 1;
>
> for (; $num; $num /= $base) {
> $ret .= (Chars)[$num % $base];
> }
> return scalar reverse $ret . '0'x($minlen - length($ret));
> }


I assume you left this as an excercise for the reader?

use constant Charstr => join '', Chars;
sub base2int {
use integer;

my ($ret, $num, $base) = (0, @_);
return if $num !~ /^[${\Charstr}]+$/o
|| ($base ||= 10) < 2
|| MaxBase < $base;

for( my $i = length($num)-1, my $c = 0; $i >= 0; --$i ) {
$ret += index(Charstr, substr($num, $i, 1)) * $base**$c++;
}
return $ret;
}

--
Brad
ps: If you care, please pardon the use of google groups.
I'm still exploring options since my employer dropped
most newsgroups.

 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      11-15-2005
Brad Baxter wrote:
> Dr.Ruud wrote:
>>#!/usr/bin/perl
>>use strict; use warnings;
>>
>>use constant Chars => ('0'..'9', 'A'..'Z', 'a'..'z');
>>use constant MaxBase => scalar Chars;
>>
>>{ local ($,, $\) = ("\t", "\n");
>>
>> for (my $i = 0; $i != 30; $i++) {
>> print $i, int2base($i, 16, 4);
>> }
>>}
>>
>>sub int2base {
>> use integer;
>>
>> my ($ret, $num, $base, $minlen) = ('', @_);
>> return undef if $num < 0
>> || ($base ||= 10) < 2
>> || MaxBase < $base
>> || ($minlen ||= 1) < 1;
>>
>> for (; $num; $num /= $base) {
>> $ret .= (Chars)[$num % $base];
>> }
>> return scalar reverse $ret . '0'x($minlen - length($ret));
>>}

>
> I assume you left this as an excercise for the reader?
>
> use constant Charstr => join '', Chars;
> sub base2int {
> use integer;
>
> my ($ret, $num, $base) = (0, @_);
> return if $num !~ /^[${\Charstr}]+$/o
> || ($base ||= 10) < 2
> || MaxBase < $base;
>
> for( my $i = length($num)-1, my $c = 0; $i >= 0; --$i ) {
> $ret += index(Charstr, substr($num, $i, 1)) * $base**$c++;
> }
> return $ret;
> }


And don't forget POSIX::strtol and POSIX::strtoul.

$ perl -MPOSIX -le'print strtoul "ZZ", 36'
12950



John
--
use Perl;
program
fulfillment
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Page File counter and Private Bytes Counter George2 C++ 1 01-31-2008 09:27 AM
Session("counter") vs. ViewState("counter")...a newbie question The Eeediot ASP .Net 3 12-22-2004 09:31 PM
Check if var is alphanumeric? VB Programmer ASP .Net 2 11-16-2004 06:46 PM
Natural sorting order for alphanumeric fields Paul Java 1 09-14-2004 12:41 PM
Easy way to specify all non-alphanumeric characters? Steven J Sobol Java 8 04-30-2004 09:15 AM



Advertisments