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

HI , I am just getting to know the basics of perl, so please bear with me... how I can display a number in the 001 002...010 011 012..020 format? thanks, BA

Replies are listed 'Best First'.
Re: displaying numbers like 001
by Jaap (Curate) on Jul 17, 2005 at 10:19 UTC
    try (s)printf:
    my $number = 3; ### print directly printf ("%03d", $number); ### or assign to a scalar my $string = sprintf ("%03d", $number);
Re: displaying numbers like 001
by ysth (Canon) on Jul 17, 2005 at 10:20 UTC
    $formatted_number = sprintf "%.3d", $number; print "Got $formatted_number!\n";
    or just
    printf "Got %.3d!\n", $number;
Re: displaying numbers like 001
by jhourcle (Prior) on Jul 17, 2005 at 17:03 UTC

    I'd personally go with a (s)printf approach, but the example you gave wasn't clear what base you intended these numbers to be -- all of the examples you gave only used the characters 0,1 and 2.

    If you tried loading those numbers into strings, and then print them, they'll get translated to base 10. If you have them unquoted, they'll be translated as base 8, because Perl uses the leading 0 to mark octal numbers:

    printf "%03d\n", '020'; # prints 020 printf "%03d\n", 020 ; # prints 016 printf "%03o\n", 020 ; # prints 020 printf "%03o\n", '020'; # prints 024
      I think you've all missed the point. The OP wants to display numbers in base 3.

        I wondered about that myself, at first - but the OP said "001 002...010 011 012..020", suggesting that there were ranges desired between 002 and 010 and between 012 and 020. In base 3, there is nothing in those ranges.

        update: just for fun, here's a solution to the base-3 problem. I dispose of the 0 padding in this case, because three places doesn't buy you much in base-3.

        use strict; use warnings; sub base3 { my $num = shift; my @res; do { unshift @res, ( $num % 3 ) } while ( $num = int( $num / 3 ) ); return join '', @res; } print base3( 65 ); __END__ 2102

        Obviously in the real world, you would do some input checking, and likely factor out the constant base. It feels like there should be a way to do this with pack...

Re: displaying numbers like 001
by pg (Canon) on Jul 17, 2005 at 16:14 UTC

    I would first choose sprintf, but can also use format, depends on your purpose.

    $number = 1; format = @0# $number . write; $number = 20;write;
Re: displaying numbers like 001
by gam3 (Curate) on Jul 17, 2005 at 16:11 UTC
    Or if you want to do something more interesting:
    for my $x ( 1, 2, 11 ) { print 0 x (3-length($x)), $x, "\n"; }
    -- gam3
    A picture is worth a thousand words, but takes 200K.
Re: displaying numbers like 001
by Adrade (Pilgrim) on Jul 18, 2005 at 05:54 UTC
    I've had much much happy success using Math::BaseCnv and its simply the easiest thing to use...
    use Math::BaseCnv; my $your_number = 55; # cnv( THE-NUMBER, OLD-BASE, NEW-BASE ); my $new_number = cnv( $your_number, 10, 3 ); # $new_number becomes 2001
    Hope this helps - good luck,
      -Adam

    --
    By a scallop's forelocks!