Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

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

by JCHallgren (Sexton)
on Jan 17, 2006 at 04:21 UTC ( [id://523661]=note: print w/replies, xml ) Need Help??


in reply to Re: Left padding a number with zeros: PerlFaq method didn't work for me.
in thread Left padding a number with zeros: PerlFaq method didn't work for me.

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?
  • Comment on Re^2: Left padding a number with zeros: PerlFaq method didn't work for me.
  • Download Code

Replies are listed 'Best First'.
Re^3: Left padding a number with zeros: PerlFaq method didn't work for me.
by helphand (Pilgrim) on Jan 17, 2006 at 04:31 UTC

    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

Re^3: Left padding a number with zeros: PerlFaq method didn't work for me.
by ChemBoy (Priest) on Jan 17, 2006 at 04:45 UTC

    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

Re^3: Left padding a number with zeros: PerlFaq method didn't work for me.
by McDarren (Abbot) on Jan 17, 2006 at 04:46 UTC
    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 :)

      Ok...thanks to those who replied and explained why my syntax was warped! Now I feel a bit better! Given that I have a total of maybe 2 weeks (and 3 pgrms) using Perl, at best, y'all may thus understand why I read it the way I did...

Log In?
Username:
Password:

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

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

    No recent polls found