Expand Messages
• Hi there, I am hoping to make the following changes to integers. I would like to do something like the following sprintf command Below but instead of leading
Message 1 of 6 , Nov 7 3:07 PM
Hi there,

I am hoping to make the following changes to integers.
I would like to do something like the following sprintf command Below

\$value = sprintf ("%04d", \$value);

1 to become 1000
23 to become 2300

is there a way to do that with the sprintf command.
• ... You can t. Read FAQ 4: How do I pad a string with blanks or pad a number with zeroes?
Message 2 of 6 , Nov 7 4:25 PM
On 11/7/2012 5:07 PM, Noah wrote:
> Hi there,
>
> I am hoping to make the following changes to integers.
> I would like to do something like the following sprintf command Below

How do I pad a string with blanks or pad a number with zeroes?

The answer is near the bottom.

Charles Clarkson
--
I'm not really a smart person. I just play one on the Internet.
• my \$value = 3; sub suffix_0 { my \$c = \$_[0] - length(\$_[1]); if (\$c 0) { my \$result = \$value . 0 x\$c; return \$result; } return \$_[1]; } print suffix_0(5,
Message 3 of 6 , Nov 8 4:40 AM
my \$value = 3;

sub suffix_0 {
my \$c = \$_[0] - length(\$_[1]);
if (\$c>0) {
my \$result = \$value . '0'x\$c;
return \$result;
}
return \$_[1];
}

print suffix_0(5, \$value);

At 2012-11-08 07:07:53,Noah <noah-list@...> wrote:

Hi there,

I am hoping to make the following changes to integers.
I would like to do something like the following sprintf command Below

\$value = sprintf ("%04d", \$value);

1 to become 1000
23 to become 2300

is there a way to do that with the sprintf command.

[Non-text portions of this message have been removed]
• ... Huangj my \$value = 3; Huangj sub suffix_0 { Huangj my \$c = \$_[0] - length(\$_[1]); Huangj if (\$c 0) { Huangj my \$result = \$value . 0 x\$c; Huangj
Message 4 of 6 , Nov 8 5:15 AM
>>>>> "Huangj" == Huangj <redspid@...> writes:

Huangj> my \$value = 3;
Huangj> sub suffix_0 {
Huangj> my \$c = \$_[0] - length(\$_[1]);
Huangj> if (\$c>0) {
Huangj> my \$result = \$value . '0'x\$c;
Huangj> return \$result;
Huangj> }
Huangj> return \$_[1];
Huangj> }

Huangj> print suffix_0(5, \$value);

Well, that's one sideways way to do it. Especially for Perl. :)

I'd just do this:

\$num = sprintf('%4d', \$num); # space pad to 4 chars
\$num =~ tr/ /0/; # turn spaces into zeroes

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.posterous.com/ for Smalltalk discussion
• Hi Huangj, a few comments on your code. On Thu, 8 Nov 2012 20:40:25 +0800 (CST) ... \$value is global to the subroutine. That s not good. ... You should not use
Message 5 of 6 , Nov 8 5:38 AM
Hi Huangj,

On Thu, 8 Nov 2012 20:40:25 +0800 (CST)
Huangj <redspid@...> wrote:

> my \$value = 3;
>
>
> sub suffix_0 {

\$value is global to the subroutine. That's not good.

> my \$c = \$_[0] - length(\$_[1]);

You should not use positional indexes into @_:

> if (\$c>0) {
> my \$result = \$value . '0'x\$c;
> return \$result;
> }
> return \$_[1];
> }

Why are you assigning to \$result and then doing nothing with it except
returning it? Furthermore, if \$c <=0 then '0'x\$c will be the empty string which
is also OK.

And you should not reference \$value inside the loop.

I would write it as:

sub suffix_0 {
my (\$len, \$s) = @_;

return \$s . ('0' x (\$len - length(\$s)));
}

If you want to be on the safe side you can use ? : to check for
zero-or-negative values.

Regards,

Shlomi Fish

--
-----------------------------------------------------------------
Shlomi Fish http://www.shlomifish.org/
"The Human Hacking Field Guide" - http://shlom.in/hhfg

If Botticelli were alive today, he’d be working for Vogue.
http://en.wikiquote.org/wiki/Peter_Ustinov

• I am glad that my code received so many replies. I learn from your suggestions to learn many things. Thank you. so, i will coding more code, post to the
Message 6 of 6 , Nov 8 10:50 PM
I learn from your suggestions to learn many things.
Thank you.
so, i will coding more code, post to the mailist.
At 2012-11-08 21:38:45,"Shlomi Fish" <shlomif@...> wrote:
>Hi Huangj,
>
>
>On Thu, 8 Nov 2012 20:40:25 +0800 (CST)
>Huangj <redspid@...> wrote:
>
>> my \$value = 3;
>>
>>
>> sub suffix_0 {
>
>\$value is global to the subroutine. That's not good.
>
>> my \$c = \$_[0] - length(\$_[1]);
>
>You should not use positional indexes into @_:
>
>
>> if (\$c>0) {
>> my \$result = \$value . '0'x\$c;
>> return \$result;
>> }
>> return \$_[1];
>> }
>
>Why are you assigning to \$result and then doing nothing with it except
>returning it? Furthermore, if \$c <=0 then '0'x\$c will be the empty string which
>is also OK.
>
>And you should not reference \$value inside the loop.
>
>I would write it as:
>
>sub suffix_0 {
> my (\$len, \$s) = @_;
>
> return \$s . ('0' x (\$len - length(\$s)));
>}
>
>If you want to be on the safe side you can use ? : to check for
>zero-or-negative values.
>
>Regards,
>
> Shlomi Fish
>
>--
>-----------------------------------------------------------------
>Shlomi Fish http://www.shlomifish.org/
>"The Human Hacking Field Guide" - http://shlom.in/hhfg
>
>If Botticelli were alive today, he’d be working for Vogue.
> — http://en.wikiquote.org/wiki/Peter_Ustinov
>