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

20188 18350 4005 127736 10291 4937 57500 150494
I have a file entitled numbers.txt. It comprises the numbers listed above. Using perl, how would place leading zero in front of each line; so the length of each line will not be greater than 10. I tried using scalars in perl, but the value of the scalar only returns the numbers of elements in the file when the file is assigned to an array.
The following is the need output: 0000020188 0000018350 0000004005 0000127736 0000010291 0000004937 0000057500 0000150494
Please help!

Edit by tye

Replies are listed 'Best First'.
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.";
    
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.
      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

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

      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 :-)
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. ;)

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!

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.
      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);'
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) {

      Oh! You must mean some like this:

      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.