cztmonk has asked for the wisdom of the Perl Monks concerning the following question:

What does the following code do?

use Acme::Tools; # num2code() can be used to compress numeric IDs to # something shorter: $chars='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz +-_'; $code=num2code("241274432",5,$chars);
it produces the following output: EOOv0

I don't understand it...

Replies are listed 'Best First'.
Re: acme::tools num2code function
by moritz (Cardinal) on Jun 11, 2012 at 08:51 UTC

    It took me a while to understand what num2code does, so let me try to explain a bit better:

    If you call num2code($number, $output_length, $chars), it converts $number to base length($chars) using the characters in $chars as symbol of the output alphabet. Then the result is padded to $output_length characters.

    So if you call it as num2code($number, 10, 'AB'), it will produce a string of 10 characters consisiting of A's and B's, where A means 0 and B means 1.

      Thanks, good explanation. I've learned something new today.

      $number=5; $code=num2code($number, 10, 'AB'), print $code ,"\n";

      it results in this output:

      AAAAAAABAB

      thanks again

        I've learned something new today

        Along the same line of thought, you may be interested in Math::BaseCnv

        #!/usr/bin/perl use warnings; use strict; use Math::BaseCnv; #fast # Convert 63 from base-10 (decimal) to base-2 (binary) # $binary_63 = cnv( 63, 10, 2 ); my $time = time; print "time -> $time\n"; my $time_128 = cnv( $time, 10, 128 ); print "time_128 -> $time_128\n"; #this will sometimes print hidden newlines my $time_10 = cnv( $time_128, 128, 10 ); print "time_10 -> $time_10\n"; my $time_64 = cnv( $time, 10, 64 ); print "time_64 -> $time_64\n"; my $time_b64 = cnv( $time, 10, 'b64' ); print "time_b64 -> $time_b64\n"; exit;

        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku ................... flash japh
Re: acme::tools num2code function
by davido (Cardinal) on Jun 11, 2012 at 08:43 UTC

    The code you showed passes a string of numeric digits, a single-digit integer, and a string of characters held in $chars to a funtion named num2code, capturing the output into $code. You didn't show the line that actually produces output.

    I can look at the output and formulate a theory as to what num2code does, but why don't you look at the documentation for num2code, or the source code for the function, and then follow up here if you still have a question.


    Dave

      I will look at the sourcecode and try to understand.
Re: acme::tools num2code function
by Corion (Patriarch) on Jun 11, 2012 at 08:40 UTC

    Have you looked at the Acme::Tools documentation? It explains what the subroutine is supposed to do. What parts of the documentation are unclear to you?

      Yes I did. I did not understand the resulting output. (EOOv0).

        The first sentence of the documentation for code2num and num2code says:

        num2code() convert numbers (integers) from the normal decimal system to some arbitrary other number system.

        There are more examples of what the functions do. What parts exactly are you having problems with? If you suspect a bug in either of the functions, please show the result you expect and the result you get.