Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

sprintf to zero-pad on the right

by Anonymous Monk
on Mar 15, 2004 at 16:24 UTC ( [id://336744]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Newbie sprintf question. I have a string TEST and I want to append zeros so that I have TEST0000.

Trying the following yields 0000TEST:

my $string = 'TEST'; $string = sprintf "%08s", $string;

So after reading a bit more, I tried this thinking that the zeros would appear after the string:

my $string = 'TEST'; $string = sprintf "%-08s", $string;

But that gives me "TEST    " (note the 4 blank spaces instead of zeros). I'm sure I'm missing something obvious.

janitored by ybiC: Retitle from "sprintf question" for better search results

Replies are listed 'Best First'.
Re: sprintf question
by matija (Priest) on Mar 15, 2004 at 16:43 UTC
    How about $string.="0" x (8-length($string))
Re: sprintf question
by kvale (Monsignor) on Mar 15, 2004 at 16:27 UTC
    How about avoiding sprintf altogether?
    my $string = 'TEST'; $string .= '0000';
    Update: If you have a variable length string, then use
    $string .= '0' x ($fixed_length - length $string);
    to pad it to the desired $fixed_length.

    -Mark

      Hi Mark, thanks for your reply and I should have explained further.

      The total length I'm looking for is 8. So if I see a four character string (i.e TEST) I know I should append 4 zeros. If I see a three character string, I would append 5 zeros etc.

      I thought sprintf would be a natural choice for this but couldn't get it to work...

Re: sprintf question
by Tomte (Priest) on Mar 15, 2004 at 16:46 UTC

    I really don't know if there's not a much better solution, but this does the trick:

    reverse(sprintf("%08s", "" . reverse( "TEST")));

    regards,
    tomte


    Hlade's Law:

    If you have a difficult task, give it to a lazy person --
    they will find an easier way to do it.

Re: sprintf question
by BrowserUk (Patriarch) on Mar 15, 2004 at 19:20 UTC

    $string = 'TEST'; print substr $string . '0'x8, 0, 8; TEST0000

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
Re: sprintf question
by TomDLux (Vicar) on Mar 15, 2004 at 19:08 UTC

    I would normally do what matija & kvale suggest. I've also seen code that appends $field_size zeros to the print string, and then taking a substring:

    $padding ="0" x $field_size; $padded_string = substring $text . $padding, 0, $field_size;

    If you have a humonguous number of fields to output, it might be worth benchmarking the two methods.

    --
    TTTATCGGTCGTTATATAGATGTTTGCA

Re: sprintf question
by AcidHawk (Vicar) on Mar 16, 2004 at 08:45 UTC
    What about using substr..?
    my $strlen = "00000000"; my $string = "TEXT"; substr($strlen, -length($string)) = "$string";

    You can then print "$strlen\n";

    -----
    Of all the things I've lost in my life, its my mind I miss the most.
Re: sprintf question
by ysth (Canon) on Mar 16, 2004 at 03:37 UTC
    $string = reverse sprintf "%08s", scalar reverse $string;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://336744]
Approved by kvale
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2024-04-19 09:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found