Format a number with any leading arbitrary character

Discussion in 'Perl Misc' started by jidanni@jidanni.org, Jul 23, 2009.

  1. Guest

    $ 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.
    , Jul 23, 2009
    #1
    1. Advertising

  2. Guest

    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
    , Jul 24, 2009
    #2
    1. Advertising

  3. C.DeRykus Guest

    On Jul 23, 1:24 pm, 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
    C.DeRykus, Jul 24, 2009
    #3
  4. 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
    Eric Pozharski, Jul 24, 2009
    #4
  5. Steve C Guest

    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
    Steve C, Jul 24, 2009
    #5
  6. 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
    John W. Krahn, Jul 24, 2009
    #6
  7. Guest

    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
    , Jul 24, 2009
    #7
  8. Guest

    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".
    , Jul 24, 2009
    #8
  9. Guest

    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
    , Jul 24, 2009
    #9
  10. Steve C Guest

    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?
    Steve C, Jul 24, 2009
    #10
  11. Guest

    On Sat, 25 Jul 2009 02:17:12 +0800, 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".


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

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


    -sln
    , Jul 24, 2009
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Honestmath
    Replies:
    5
    Views:
    548
    Honestmath
    Dec 13, 2004
  2. John Nagle
    Replies:
    4
    Views:
    574
    James Antill
    Aug 7, 2007
  3. Delaney, Timothy (Tim)
    Replies:
    0
    Views:
    555
    Delaney, Timothy (Tim)
    Aug 7, 2007
  4. Ken Starks
    Replies:
    4
    Views:
    325
    Ken Starks
    Jun 23, 2008
  5. Replies:
    4
    Views:
    107
Loading...

Share This Page