Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Format a number with any leading arbitrary character

Reply
Thread Tools

Format a number with any leading arbitrary character

 
 
jidanni@jidanni.org
Guest
Posts: n/a
 
      07-23-2009
$ 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.
 
Reply With Quote
 
 
 
 
sln@netherlands.com
Guest
Posts: n/a
 
      07-24-2009
On Fri, 24 Jul 2009 04:24:24 +0800, wrote:

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

 
Reply With Quote
 
 
 
 
C.DeRykus
Guest
Posts: n/a
 
      07-24-2009
On Jul 23, 1:24*pm, jida...@jidanni.org wrote:
> $ 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;

--
Charles DeRykus
 
Reply With Quote
 
Eric Pozharski
Guest
Posts: n/a
 
      07-24-2009
On 2009-07-23, <> wrote:
> $ 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.



--
Torvalds' goal for Linux is very simple: World Domination
Stallman's goal for GNU is even simpler: Freedom
 
Reply With Quote
 
Steve C
Guest
Posts: n/a
 
      07-24-2009
wrote:
> $ 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
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      07-24-2009
wrote:
> $ 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
--
Those people who think they know everything are a great
annoyance to those of us who do. -- Isaac Asimov
 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      07-24-2009
On Fri, 24 Jul 2009 04:24:24 +0800, wrote:

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

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

-sln

 
Reply With Quote
 
jidanni@jidanni.org
Guest
Posts: n/a
 
      07-24-2009
I declare
>>>>> "SC" == Steve C <> writes:

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
> perldoc -f substr
> Hint: B<substr()> returns l-value.

well, "it's probably an even better answer".
 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      07-24-2009
On Fri, 24 Jul 2009 04:24:24 +0800, wrote:

>$ 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
 
Reply With Quote
 
Steve C
Guest
Posts: n/a
 
      07-24-2009
wrote:
> I declare
>>>>>> "SC" == Steve C <> writes:

> 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
>> perldoc -f substr
>> Hint: B<substr()> returns l-value.

> 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?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
binary number format ? format character %b or similar. Ken Starks Python 4 06-23-2008 08:59 AM
format a number with leading and trailing zeros absmienk@hotmail.com Perl Misc 4 08-15-2007 01:18 PM
All leading tabs or all leading spaces - why isn't that enforced? John Nagle Python 4 08-07-2007 04:05 PM
RE: All leading tabs or all leading spaces - why isn't that enforced? Delaney, Timothy (Tim) Python 0 08-07-2007 03:50 AM
calling an arbitrary function w/ arbitrary arguments Honestmath C++ 5 12-13-2004 06:18 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57