# 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

2. ### SisyphusGuest

<> 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

3. ### Paul LalliGuest

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
4. ### SisyphusGuest

"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