Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Left padding a number with zeros: PerlFaq method didn't work for me.

by JCHallgren (Sexton)
on Jan 17, 2006 at 03:19 UTC ( #523651=perlquestion: print w/replies, xml ) Need Help??

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

In the Perldoc faq 4, http://perldoc.perl.org/perlfaq4.html#How-do-I-pad-a-string-with-blanks-or-pad-a-number-with-zeroes%3f there are two ways listed that it says are same thing...BUT...I tried the first method, and all I got was an undef result. I then did it the second way and got what I needed. My input to stmt is a 4 char max field from a POST. Maybe I did something wrong and issue is sorta moot, as it works the other way...just wondering why they might give diff results...PRESUMING that I typed correctly. Thanks.
  • Comment on Left padding a number with zeros: PerlFaq method didn't work for me.

Replies are listed 'Best First'.
Re: Left padding a number with zeros: PerlFaq method didn't work for me.
by McDarren (Abbot) on Jan 17, 2006 at 03:34 UTC
    It's virtually impossible to comment on why this didn't work for you without seeing the code that you tried.

    Perhaps you could post a snippet of code that didn't work for you.

      Ok...ever have one of those days when you start to lose track of what you did? Well, this thread may prove that! It's been a LONG day, and last nite was also, trying to get a new utility configured to work on my sys. Thus, in looking for the code that didn't work (backup file), I realized that there was a tilde (in addition to equals, so =~) in there that I must have removed VERY late last nite! So that may well explain it...

      But I do believe/think I tried it the first line way also with same failure. FAQ says "you can use an integer in place of $pad_len if you know the pad length in advance."
      $Wmmyy = sprintf("%0${4}d", $FORM{'SRmmyy'});<br> $Wmmyy = sprintf("%0*d", 4, $FORM{'SRmmyy'});

      I had the problem about 2am last nite and remembered just now that I was going to ask about it...so I posted while doing some other tasks...oops, ok?

        The FAQ is correct, you are just interpreting it wrong. The 'integer in place of $pad_len' would not mean to replace the ${pad_len} part with ${4}. Doing so mistakenly causes the sprintf to use $4, which is undefined. The correct way to replace it would be;

        $Wmmyy = sprintf("%04d", $FORM{'SRmmyy'});<br>

        Scott

        You were probably only one character away at one point...unfortunately, then you jumped the wrong direction. :-) The format you were looking for is "%04d", which is equivalent to the version in perlfaq4 if $padlen is equal to 4.

        Your mistake was that you replaced the string "pad_len" with an integer, rather than replacing the variable $pad_len (which is inserted in the string in the FAQ using braces so that perl knows you're not looking for the variable $pad_lend). Since this means that instead of inserting "4" into your string, you're inserting the value of $4, which happens to be empty, you get no padding, which I presume is the error you saw.



        If God had meant us to fly, he would *never* have given us the railroads.
            --Michael Flanders

        Ah, okay - I think I see where you may have got confused.

        In the example from perlfaq:

        $padded = sprintf("%${pad_len}s", $text);
        The variable $pad_len is written as ${pad_len}. This is to ensure that it is correctly interpreted. If it wasn't written this way, it would be interpreted as $pad_lens.

        So where it says that you may use an integer in place of $pad_len, it means that it would be written as:

        $padded = sprintf("%04d", $text);

        Cheers,
        Darren :)

        Update: Actually, after re-reading this I realised that the original code snippet I gave you was incorrect. I've corrected it.

        Also, it's worth pointing out that $pad_length in the above examples is a little misleading, as it actually refers to the total length of the resultant string, rather than the length of the padding. The best way to demonstrate this is by example:

        #!/usr/bin/perl -w use strict; my $string1 = "12345"; # 5 character string my $string2 = "1234567890"; # 10 character string my $pad_len = 10; my $padded1 = sprintf("%${pad_len}s", $string1); my $padded2 = sprintf("%${pad_len}s", $string2); print "|$padded1|\n|$padded2|\n";

        Which gives:

        | 12345| |1234567890|

        Note that the 2nd string isn't padded at all, because the length of the string is already >= than $pad_len.

        Update: blah - you're working with numbers, not strings. Disregard the previous update, and I'll crawl back into my box :)

Re: Left padding a number with zeros: PerlFaq method didn't work for me.
by esskar (Deacon) on Jan 17, 2006 at 03:33 UTC
    my $len = 10; print "1: [", sprintf("%${len}s", "*"), "]\n"; print "2: [", sprintf("%*s", $len, "*"), "]\n";
    i get what i the say

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (1)
As of 2022-10-03 01:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred way to holiday/vacation is:











    Results (13 votes). Check out past polls.

    Notices?