Re: Leading Zeros.....Please Help
by sauoq (Abbot) on Sep 27, 2002 at 16:27 UTC
|
If you are printing them directly, you might prefer to use printf instead of sprintf.
BTW, if you had simply entered "Leading Zeros" in that search box on your upper left, you would have gone directly to Leading Zeros and found that the first reply in that thread answered your question. If that hadn't worked, you could have used Super Search and found a handful of nodes that would have been helpful.
-sauoq
"My two cents aren't worth a dime.";
| [reply] |
Re: Leading Zeros.....Please Help
by zigdon (Deacon) on Sep 27, 2002 at 16:06 UTC
|
take a look at sprintf, especially at what "%010d" would resolve do.
-- Dan
updated: s/%10d/%010d/ per flounder99's post. | [reply] |
|
|
Actually you would use "%010d" to get the leading zeros.
This one-liner
perl -ne 'printf("%010d\n",$_)'<numbers.txt
will do the trick.
-- flounder | [reply] [d/l] [select] |
Re: Leading Zeros.....Please Help
by mjeaton (Hermit) on Sep 27, 2002 at 16:06 UTC
|
See sprintf
For example:
# Format number with up to 8 leading zeroes
$result = sprintf("%08d", $number);
mike
Updated: Added a real link to the perl docs. | [reply] |
Re: Adding Leading Zeros
by seattlejohn (Deacon) on Sep 27, 2002 at 18:22 UTC
|
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.
| [reply] [d/l] |
|
|
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 :-)
| [reply] [d/l] |
Re: Adding Leading Zeros
by jaxon (Novice) on Sep 27, 2002 at 23:38 UTC
|
my $short_num = 20188;
my $len = 10;
my $padded_num = sprintf ("%0${len}d", $short_num );
print $padded_num
prints:
0000020188
Of course, there is no way to make an 11 digit number's length will not be greater than 10. ;)
| [reply] |
Re: Leading Zeros.....Please Help
by Mr. Muskrat (Canon) on Sep 27, 2002 at 16:29 UTC
|
Next time you have a question, please post the code that you have tried. Then we can show you different ways of looking at the problem at hand.
By the way, welcome to the monastery!
| [reply] |
Re: Adding Leading Zeros
by AcidHawk (Vicar) on Sep 28, 2002 at 15:02 UTC
|
Don't laugh, This is some of my REALLY old code but it proves that there is ALWAYS more than one way...
$var = "0000000000";
substr($var, -length($ARGV[0])) = "$ARGV[0]";
print "$var\n";
-----
Of all the things I've lost in my life, its my mind I miss the most.
| [reply] [d/l] |
|
|
I have a silly question here. Why does the following code return 000000001739 and how do I fix this?
perl -e 'printf("%012d", 17.40 * 100);'
| [reply] |
|
|
19:35 >perl -wE "printf(qq[%012d], 17.40 * 100);"
000000001739
19:35 >perl -wE "printf(qq[%012d], int((17.40 * 100) + 0.5));"
000000001740
19:35 >
Hope that helps,
| [reply] [d/l] |
Re: Adding Leading Zeros
by Anonymous Monk on Aug 29, 2016 at 17:27 UTC
|
Simple trick with perl. use strings instead of numbers:
foreach my $index ('00'..'12') {
vs
foreach my $index (00..12) {
| [reply] [d/l] [select] |
|
|
my @inputs = (20188, 18350, 4005, 127736, 10291, 4937, 57500, 150494);
sub with_zeroes { grep( $_ == $_[0], '0000000000' .. '9999999999' ) }
say with_zeroes($_) for @inputs;
Small doubt about efficiency remains however.
| [reply] [d/l] |