Discussion:
FAQ 4.33 How do I pad a string with blanks or pad a number with zeroes?
(too old to reply)
PerlFAQ Server
2006-08-18 19:03:02 UTC
Permalink
This is an excerpt from the latest version perlfaq4.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .

--------------------------------------------------------------------

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

In the following examples, $pad_len is the length to which you wish to
pad the string, $text or $num contains the string to be padded, and
$pad_char contains the padding character. You can use a single character
string constant instead of the $pad_char variable if you know what it is
in advance. And in the same way you can use an integer in place of
$pad_len if you know the pad length in advance.

The simplest method uses the "sprintf" function. It can pad on the left
or right with blanks and on the left with zeroes and it will not
truncate the result. The "pack" function can only pad strings on the
right with blanks and it will truncate the result to a maximum length of
$pad_len.

# Left padding a string with blanks (no truncation):
$padded = sprintf("%${pad_len}s", $text);
$padded = sprintf("%*s", $pad_len, $text); # same thing

# Right padding a string with blanks (no truncation):
$padded = sprintf("%-${pad_len}s", $text);
$padded = sprintf("%-*s", $pad_len, $text); # same thing

# Left padding a number with 0 (no truncation):
$padded = sprintf("%0${pad_len}d", $num);
$padded = sprintf("%0*d", $pad_len, $num); # same thing

# Right padding a string with blanks using pack (will truncate):
$padded = pack("A$pad_len",$text);

If you need to pad with a character other than blank or zero you can use
one of the following methods. They all generate a pad string with the
"x" operator and combine that with $text. These methods do not truncate
$text.

Left and right padding with any character, creating a new string:

$padded = $pad_char x ( $pad_len - length( $text ) ) . $text;
$padded = $text . $pad_char x ( $pad_len - length( $text ) );

Left and right padding with any character, modifying $text directly:

substr( $text, 0, 0 ) = $pad_char x ( $pad_len - length( $text ) );
$text .= $pad_char x ( $pad_len - length( $text ) );



--------------------------------------------------------------------

The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in
perlfaq.pod.
--
Posted via a free Usenet account from http://www.teranews.com
bdz
2006-08-19 04:04:30 UTC
Permalink
Thanks for the explaination and examples.

To clarify: $padded = sprintf("%0*d", $pad_len, $num);

When I tried it out in a short sample program:
## begin code ###
#!/usr/bin/perl

while (-1){
print "enter number of spaces: ";
$pad_len=<STDIN>;
chomp($pad_len);
print "enter number: ";
$num=<STDIN>;
chomp($num);
$padded = sprintf("%0*d", $pad_len, $num);

print "$padded\n";
}
### end code ###

yields a string of length $pad_length with the number of zeros added to
make it that length rather than adding $pad_length zeros to the left of
a number.

Is there anything one can do to preserve the leading zeros in the
string for later use, as in a file name for example, or do they have to
be generated whenever you need the string?
A. Sinan Unur
2006-08-19 04:55:21 UTC
Permalink
Post by bdz
Thanks for the explaination and examples.
To clarify: $padded = sprintf("%0*d", $pad_len, $num);
Please quote an appropriate amount of context when you reply. I filter
out the FAQ postings, and I am not going to dig out this one to figure
out what you are thanking about or clarifying.
Post by bdz
## begin code ###
#!/usr/bin/perl
use strict;
use warnings;

missing.
Post by bdz
while (-1){
What is wrong with

while ( 1 ) {
Post by bdz
print "enter number of spaces: ";
$pad_len=<STDIN>;
chomp($pad_len);
print "enter number: ";
$num=<STDIN>;
chomp($num);
$padded = sprintf("%0*d", $pad_len, $num);
print "$padded\n";
}
Please indent your code properly to help others reading your code.
Post by bdz
yields a string of length $pad_length with the number of zeros added
to make it that length rather than adding $pad_length zeros to the
left of a number.
OK, that is what "%0*d" is supposed to do.
Post by bdz
Is there anything one can do to preserve the leading zeros in the
string for later use,
I have no idea what you are asking. You saved the generated string in
$padded.
Post by bdz
as in a file name for example, or do they have to
be generated whenever you need the string?
Use $padded. If you are asking something else, clarify (preferably,
after reading the posting guidelines).

Here is a cleaned up version of your script:

#!/usr/bin/perl

use strict;
use warnings;

$| = 1;

while ( 1 ) {
print "enter number of spaces: ";
chomp ( my $pad_len = <STDIN> );
print "enter number: ";
chomp ( my $num = <STDIN> );
printf "%0*d\n", $pad_len, $num;
}
__END__
--
--
A. Sinan Unur <***@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
Matt Garrish
2006-08-19 11:27:13 UTC
Permalink
Post by A. Sinan Unur
Post by bdz
Thanks for the explaination and examples.
To clarify: $padded = sprintf("%0*d", $pad_len, $num);
yields a string of length $pad_length with the number of zeros added
to make it that length rather than adding $pad_length zeros to the
left of a number.
OK, that is what "%0*d" is supposed to do.
I think he doesn't want s/printf at all, but just

my $padded = '0'x$pad_len . $num;

But in my opinion that just seems ugly and pointless
Post by A. Sinan Unur
Post by bdz
Is there anything one can do to preserve the leading zeros in the
string for later use,
I have no idea what you are asking. You saved the generated string in
$padded.
Got me there too, unless he's doing something dumb like padding the
number before performing other math operations on it so that they're
subsequently lost. I suppose that could be solved in this case
(assuming I'm right above) by the following:

my $pad = '0'x$pad_len;

And then applying the pad whenever outputting.

Matt

Loading...