any simple way to do this?

Discussion in 'Perl Misc' started by robertchen117@gmail.com, Apr 9, 2007.

  1. Guest

    the day of the date is a number, April 4.

    I want to replace the number with 04, if it is already 04, then do
    nothing. If it is 4 replace with 04.

    Please give me a simple way to do this.
    , Apr 9, 2007
    #1
    1. Advertising

  2. Sisyphus Guest

    <> wrote in message
    news:...
    > the day of the date is a number, April 4.
    >
    > I want to replace the number with 04, if it is already 04, then do
    > nothing. If it is 4 replace with 04.
    >
    > Please give me a simple way to do this.
    >


    Does this help ?

    ---------------
    use strict;
    use warnings;

    for(04, 4, 14, 214) {printf "%02d ", $_}

    print "\n";

    #or use sprintf() if you want to assign:

    for(04, 4, 24, 4132) {
    my $formatted = sprintf( "%02d", $_);
    print $formatted, " ";
    }

    print "\n";
    ------------------

    Cheers,
    Rob
    Sisyphus, Apr 9, 2007
    #2
    1. Advertising

  3. Paul Lalli Guest

    On Apr 9, 5:44 am, "Sisyphus" <> wrote:
    > <> wrote in message
    >
    > news:...
    >
    > > the day of the date is a number, April 4.

    >
    > > I want to replace the number with 04, if it is already 04, then do
    > > nothing. If it is 4 replace with 04.

    >
    > > Please give me a simple way to do this.

    >
    > Does this help ?
    >
    > ---------------
    > use strict;
    > use warnings;
    >
    > for(04, 4, 14, 214) {printf "%02d ", $_}
    >
    > print "\n";
    >
    > #or use sprintf() if you want to assign:
    >
    > for(04, 4, 24, 4132) {

    ^^

    Be careful. That worked only because Decimal 4 happens to be
    equivalent to Octal 4. Try it with 09 or 010 and see what happens.

    Paul Lalli
    Paul Lalli, Apr 9, 2007
    #3
  4. Sisyphus Guest

    "Paul Lalli" <> wrote in message
    ..
    ..
    >> for(04, 4, 24, 4132) {

    > ^^
    >
    > Be careful. That worked only because Decimal 4 happens to be
    > equivalent to Octal 4. Try it with 09 or 010 and see what happens.
    >


    Damned octal :)

    s/04/'09'/

    Cheers,
    Rob
    Sisyphus, Apr 10, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    7
    Views:
    506
    Neredbojias
    Jan 21, 2006
  2. Peter Knoerrich
    Replies:
    9
    Views:
    293
    Peter Knoerrich
    Jul 1, 2004
  3. John_Woo
    Replies:
    0
    Views:
    344
    John_Woo
    Aug 15, 2006
  4. Yarik
    Replies:
    2
    Views:
    351
    Yarik
    Aug 25, 2006
  5. Replies:
    6
    Views:
    251
    Joe Wright
    Mar 10, 2007
Loading...

Share This Page