Ing. Branislav Gerzo wrote:
John W. Krahn [JWK], on Wednesday, March 30, 2005 at 12:56 (-0800)
thoughtfully wrote the following:
JWK> You could use the Math::BaseCalc module:
JWK> http://search.cpan.org/~kwilliams/Math-BaseCalc-1.011/
thanks, it is nice module, really easy to use. And also thanks to your
program, but I have some questions:
- my goal is 'convert' numbers to characters, so:
my @digits = ( "a" .. "z", "A" .. "Z", 0 .. 9, "-", "_" );
1 => 'a'
63 => '_'
64 => 'aa'
65 => 'ab'
and so on.
No, that is wrong.
0 => 'a'
1 => 'b'
63 => '_'
64 => 'ba'
65 => 'bb'
That module, and also your nice program get these result:
64 => 'ba'
65 => 'bb'
I understand why it is so...
So that means I lost some combination via base module like 'a*' and so
on. I want this stuff to encode URLs, I have in database some numbers,
get max number, encode it via this function, and have 'new url'. I
know, there is lots stuff like this on the net, but I'd like to code
my own for my project (it is not about compressing url, it is only
feature:)
So, if you could be so kind and if you have time, you can rewrite my
program (surely could be improved) to get same results, here is the
latest version I just wrote, also I did same speed improvements:
use strict;
use warnings;
my $num = 10_000_000; # $a >= 0
my @chars = ( 'a' .. 'z', 'A' .. 'Z', 0 .. 9, '-', '_' );
my $all = @chars;
print my $out = reverse(fn($num));
sub fn {
my $num = shift;
my $tmp = ($num/$all)-1;
my $out = $chars[$num % $all];
$out .= $tmp >= $all ? fn($tmp) : $chars[$tmp] if $tmp >= 0;
return $out;
}
Try your algorithm with:
my @chars = ( 0 .. 9 );
The value returned from fn() _should be exactly the same_ as the value of $num
$ perl -le'
my $num = shift;
{ my @digits = ( 0 .. 9 );
sub fn {
my $num = shift;
my $tmp = ( $num / @digits ) - 1;
my $out = $digits[ $num % @digits ];
$out .= $tmp >= @digits ? fn( $tmp ) : $digits[ $tmp ] if $tmp >= 0;
return $out;
}
}
my $out = reverse fn( $num );
print $out;
' 1234567890
0123456780
Two different digits are converted to 0?
$ perl -le'
my $num = shift;
{ my @digits = ( 0 .. 9 );
sub fn {
my $num = shift;
my $tmp = ( $num / @digits ) - 1;
my $out = $digits[ $num % @digits ];
$out .= $tmp >= @digits ? fn( $tmp ) : $digits[ $tmp ] if $tmp >= 0;
return $out;
}
}
my $out = reverse fn( $num );
print $out;
' 11
01
The same digit is converted to two different values?
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>