# Format a number with any leading arbitrary character

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
\$ perldoc -f sprintf
# Format number with up to 8 leading zeroes
\$result = sprintf("%08d", \$number);
OK, but how do I do
# Format number with up to 8 leading underscores
or any arbitrary character?
OK, I figured it out,
\$ perl -wle '\$_ = "00000050"; print; while (s/(^0*)0/\$1_/) { }; print;'
00000050
______50
Geez.

## Re: Format a number with any leading arbitrary character

On Fri, 24 Jul 2009 04:24:24 +0800, jidanni@jidanni.org wrote:

Yeah, that will work. For something more exotic ...

Convert the first 1-8 preceding 0's to underscores (leaving the columns intact):

perl -wle "\$_ = '0000000000050'; print; s/(0)([^0])/('_' x length \$1).
\$2/e; print;"
0000000000050
000________50

Or, same as above except truncating leading 0's greater than the 8 count (less
than 8, columns intact):

perl -wle "\$_ = '000000000000050'; print; s/0*?(0)([^0])/('_' x length \$1).
\$2/e; print;"
000000000000050
________50

Notice there is no '^' start of line in there. You could add that too.

-sln

## Re: Format a number with any leading arbitrary character

On Jul 23, 1:24=A0pm, jida...@jidanni.org wrote:

Or more generally if you need to emulate sprintf's
padding for shorter strings such as:  \$_ = "000050":

s/^(0*)/"0" x (length(\$1) + 8 - length \$_)/e;

--
Charles DeRykus

## Re: Format a number with any leading arbitrary character

What a strange way to express thoughts.  Me feels suspicious.

perldoc -f substr

Hint: B<substr()> returns l-value.

--
Torvalds' goal for Linux is very simple: World Domination
Stallman's goal for GNU is even simpler: Freedom

## Re: Format a number with any leading arbitrary character

jidanni@jidanni.org wrote:

Use tr for character-to-character mapping.

perl -e '\$_ = sprintf "%8d",50; tr/ /_/; print'
______50

## Re: Format a number with any leading arbitrary character

I declare
SC> Use tr for character-to-character mapping.
SC> perl -e '\$_ = sprintf "%8d",50; tr/ /_/; print'
SC> ______50
the winner, for simplest answer. As far as

well, "it's probably an even better answer".

## Re: Format a number with any leading arbitrary character

jidanni@jidanni.org wrote:

Yeah. I like substr better, too.  Why generate the wrong
value and translate when you can generate the right thing
in one step?

## Re: Format a number with any leading arbitrary character

On Sat, 25 Jul 2009 02:17:12 +0800, jidanni@jidanni.org wrote:

Its not only simple, its unessesary. Why call sprintf and  tr///
at all ?

'_' x (8-length) . \$_

-sln

## Re: Format a number with any leading arbitrary character

jidanni@jidanni.org wrote:

\$ perl -wle '\$_ = 50; print; substr \$_, 0, 0, "_" x ( 8 - length ); print;'
50
______50

John
--
Those people who think they know everything are a great
annoyance to those of us who do.        -- Isaac Asimov

## Re: Format a number with any leading arbitrary character

On Fri, 24 Jul 2009 04:24:24 +0800, jidanni@jidanni.org wrote:

I guess you could still use printf without having to do regex.
Following the 8-length strategy:

perl -wle "\$_ = 50; print; printf (\"%s%d\", '_' x (8 - length), \$_);"
50
______50

Or, it could be generalized:
----------------------
use strict;
use warnings;

my \$number = 5000;
my \$result = sprintf ("%s%d", '_' x (8 - length \$number), \$number);

print "\$number\n\$result\n";
printf ("%s%d \n", fmt('_',13,\$number), \$number);
printf ("%s%d \n", fmt('_',2,\$number), \$number);
printf ("%s%d \n", fmt(',',13,\$number), \$number);

sub fmt {
\$_[0] x (\$_[1] - length \$_[2])
}

__END__
5000
____5000
_________5000
5000
,,,,,,,,,5000

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

-sln

## Re: Format a number with any leading arbitrary character

On Fri, 24 Jul 2009 04:24:24 +0800, jidanni@jidanni.org wrote:

But err, still though, you don't need printf or the regex for what your
problem statement is. Unless you doing some other conversions at the same time.
It really has nothing to do with a 'number' at this point given Perl variable
transparency, it only has to do with formatting of data.

perl -wle "\$_ = 50; print; print '_' x (8 - length), \$_;"
50
______50

If you were to be using printf for some different formatting then all
bets are off and you will need post printf processing, probably with a regex.

Or, just simply:
\$result = sprintf("%s is a char padded integer, %3.2f is a formatted float",
'_' x (8 - length \$number), \$float);

-sln