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

Fellow Monks,

Is there a way to write a range of numbers so that it reads 01, 02, 03, etc.? Similar to [1..30], but with the added zero for the single digit. I tried [01..30] but that just gives 1, 2, 3, etc. I need it in this format because are logs are saved in this format: 03Nov01.

Thanks,
-Dru

Replies are listed 'Best First'.
Re: Range of numbers with 0 added for single digits.
by Fastolfe (Vicar) on Nov 28, 2001 at 02:29 UTC
    See sprintf. You probably want the %02d format.
Re: Range of numbers with 0 added for single digits.
by ehdonhon (Curate) on Nov 28, 2001 at 02:29 UTC

    Make them strings.

    foreach $i ( '01' .. '11' ) { print "$i "; } OUTPUT: 01 02 03 04 05 06 07 08 09 10 11
Re: Range of numbers with 0 added for single digits.
by dws (Chancellor) on Nov 28, 2001 at 02:29 UTC
    Is there a way to write a range of numbers so that it reads 01, 02, 03, etc.?

    Look to printf (or sprintf) in perlman:perlfunc, and note the effect of a zero after the '%' in the format string (e.g., "%02d").

Re: Range of numbers with 0 added for single digits.
by Anonymous Monk on Nov 28, 2001 at 05:04 UTC
    Beware of that 01 is oct(1). So 011 is oct(11) is 9.
    Writing e.g. 08 is a compile time error.
Re: Range of numbers with 0 added for single digits.
by I0 (Priest) on Nov 28, 2001 at 13:25 UTC
    ['01'..'30']
      Ah. Now that seems to be the easiest and cleaniest way. Thanks for the help.
Re: Range of numbers with 0 added for single digits.
by Aighearach (Initiate) on Nov 28, 2001 at 11:08 UTC
    foreach $n ( map { sprintf "%02d", $_ } 0..30 ) { # do stuff with $n }
    same thing, different color
    my @list = map { sprintf "%02d", $_ } 0..99;

    --
    Snazzy tagline here