Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Adding Leading Zeros

by seattlejohn (Deacon)
on Sep 27, 2002 at 18:22 UTC ( [id://201290]=note: print w/replies, xml ) Need Help??


in reply to Adding Leading Zeros

Of course the sprintf solution is completely sensible, but in the interest of TMTOWTDI and a little extra exposure for the poor underappreciated x operator, this will work too:
print "0" x (10-length($thing)) . $thing;

And yes, if length($thing) should happen to be larger than 10, the x treats the negative second operand as if it were zero.

Replies are listed 'Best First'.
Re: Re: Adding Leading Zeros
by DaveH (Monk) on Sep 28, 2002 at 23:14 UTC
    Here is a one-liner using a slight variation on your "x" theme:
    $ cat nums.txt 20188 18350 4005 127736 10291 4937 57500 150494 $ perl "-F *" -ape "s//$|x(11-@F)/e" nums.txt 0000020188 0000018350 0000004005 0000127736 0000010291 0000004937 0000057500 0000150494

    -- Dave :-)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2024-04-18 05:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found