Format a number with any leading arbitrary character

J

jidanni

$ 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.
 
S

sln

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

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{1,8})([^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{1,8})([^0])/('_' x length $1). $2/e; print;"
000000000000050
________50

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

-sln
 
C

C.DeRykus

$ 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

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

Eric Pozharski

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

What a strange way to express thoughts. Me feels suspicious.
OK, I figured it out,
$ perl -wle '$_ = "00000050"; print; while (s/(^0*)0/$1_/) { }; print;'
00000050
______50

perldoc -f substr

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

Steve C

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

Use tr for character-to-character mapping.

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

John W. Krahn

$ 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

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



John
 
S

sln

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

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
 
J

jidanni

I declareSC> 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
perldoc -f substr
Hint: B<substr()> returns l-value.
well, "it's probably an even better answer".
 
S

sln

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

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
 
S

Steve C

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

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

sln

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

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

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


-sln
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top