#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 1999
    Posts
    9
    Rep Power
    0
    I am new to Perl. Trying to print out a number padded with leading zeros.

    example:
    $num = 123;

    the output/print should have 00123.Total width of 5 padded with leading zeros.
    Appreciate any help in this direction.

    Thank you

  2. #2
  3. Ole` Timer
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2000
    Location
    N.W. Iowa
    Posts
    472
    Rep Power
    14

    Thumbs up


    Originally posted by kumars
    I am new to Perl. Trying to print out a number padded with leading zeros.

    example:
    $num = 123;

    the output/print should have 00123.Total width of 5 padded with leading zeros.
    Appreciate any help in this direction.

    Thank you

    Try this:
    Code:
    my $num = "123"; 
    $num = sprintf("%5d", $num);
    $num=~ tr/ /0/;
    this will "pad" the variable $num with leading "0"'s up to 5 characters: IE 00123 or 01234. If for some reason the $num contains a total of 5 characters, then no "0"'s would be added.

    Hope this helps

    Mickalo

    Thunder Rain Internet Publishing

    Custom Programming & Database development
    Providing Personal/Business
    Internet Solutions that work!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 1999
    Posts
    9
    Rep Power
    0

    Smile Great, it worked.


    Thank you Mickalo,

    It worked.Thanks again for the fast response.
  6. #4
  7. Ole` Timer
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2000
    Location
    N.W. Iowa
    Posts
    472
    Rep Power
    14

    Thumbs up


    Hey,.. No problems

    Glad to help when I can.

    Mickalo

    Thunder Rain Internet Publishing

    Custom Programming & Database development
    Providing Personal/Business
    Internet Solutions that work!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2007
    Posts
    1
    Rep Power
    0
    Originally Posted by mickalo
    Try this:
    Code:
    my $num = "123"; 
    $num = sprintf("%5d", $num);
    $num=~ tr/ /0/;
    Code:
    my $num = "123"; 
    $num = sprintf("%05d", $num);
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2007
    Posts
    3
    Rep Power
    0
    You can do "substr("00000$num",-5)" as well.
    It might be a bit faster though it won't be attractive.
  12. #7
  13. Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Oct 2004
    Location
    Sunny Southern California
    Posts
    2,687
    Rep Power
    862
    Originally Posted by Correction
    Code:
    my $num = "123"; 
    $num = sprintf("%05d", $num);

    hehehe... you corrected a 6 year old post! That must be a record.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2010
    Posts
    1
    Rep Power
    0
    Originally Posted by KevinADC
    hehehe... you corrected a 6 year old post! That must be a record.
    It helped me.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    1
    Rep Power
    0
    Originally Posted by wdrev
    It helped me.
    3 Years later, It helped me too
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    1
    Rep Power
    0
    It helped me too
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Location
    Paris area, France
    Posts
    842
    Rep Power
    496
    Hi,
    well I had a very similar problem today at work and I originally used a solution similar to what I thought was the best solution shown so far:

    Perl Code:
    my $num = "123"; 
    $num = sprintf("%05d", $num);


    And it does work as long as you are guaranteed to have digits in your input. Except that when I ran the program, I got an error message because there were a couple of cases where I have letters rather than digits in my input file. And the sprinft formatting string fails in this case. These letters are not really an error, but the indication of a special exception condition that still needs to be handled.

    So I think I'll change it to:

    Perl Code:
    my $num = "123"; 
    $num = sprintf("%05s", $num);


    so that if my input is "foo", I'll get "00foo" which, even if it does not seem to make too much sense, is what I need in this case to have the logic of my program working in this case (I need to pad my data with 0's at the beginning of my value, in order to be able to compare data from 2 different files with similar values but different formatting conventions.

    This was just to say that the "%05s" format string might just be more general than "%05d" in some real life situations. I haven't tested it against my data yet, I'll do that tomorrow morning.

IMN logo majestic logo threadwatch logo seochat tools logo