in reply to Regular Expression

As usual, there is more than one way...

Here is a simple, low-overhead way:

my $month=7; $month < 10 and $month = '0' . $month; print $month;
Update Oops .. Corrected, as per Dietz (++) below. Thanks.

    Earth first! (We'll rob the other planets later)

Replies are listed 'Best First'.
Re^2: Regular Expression
by tinita (Parson) on Aug 05, 2004 at 19:28 UTC
    NetWallah,
    $month < 10 and $month = '0' . $month;
    guess what's the result if my $month="07";
    =)
      Ack! - this was intended to be a simple case handler.

      If the expression starts getting any more complicated, I'd much prefer the "printf" of "sprintf" solution - that is a lot cleaner.

          Earth first! (We'll rob the other planets later)

      Fun :)
      How about:
      substr($month,0,1,"0$month") if ($month<10 && $month!~/^0/);
      Update: Fixed typo, as per tinita

      --
      Olivier
        substr($month,0,1,"0$month") if ($month<10 && $month!=/^0/);
        Use of uninitialized value in pattern match (m//) at -e line 3. 0077

        s/ != / !~ /x
        ;-)

Re^2: Regular Expression
by Dietz (Curate) on Aug 05, 2004 at 16:01 UTC
    $month > 9 and $month = '0' . $month;

    I think you meant $month < 10 and $month = '0' . $month; since 0 should be added only if number is less than 10.
    Anyway, very nice solution and I ++'d you.