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

I want to take a string and format the leading integer part to four decimal places with leading 0s ('42a' should become '0042'). I considered regex parsing the string to get the leading digits, but that seemed unnecessary because printf and sprintf should DWIM and numify the string that way anyway.

But they didn't. The '%d' format didn't cause numification. Oddly enough the '%f' format did numify:

This is perl, v5.6.0 built for i386-linux, so I tried to scan some online perldelta to see if this has been addressed. I didn't find anything, but I'm not really confident that I looked in all the right places.

So has *printf numification changed? And what resource is best, easiest, to find this out?

YuckNummy

#!/usr/bin/perl use strict; my $var = '42a'; my $tmp = $var; printf "'%%d' output : %4.4d\n", $tmp; my $tmp = $var; printf "'%%d' output (with +0): %4.4d\n", $tmp + 0; my $tmp = $var; printf "'%%f' output : %04.0f\n", $tmp; __END__ '%d' output : 0000 '%d' output (with +0): 0042 '%f' output : 0042

Replies are listed 'Best First'.
Re: printf numification
by ikegami (Patriarch) on Sep 27, 2004 at 18:05 UTC

    Are you sure "%4.4d" is legal? (Update: perlfunc says it's only legal for floating point formatters.) Using a proper formatting string, it works fine for me in ActivePerl 5.6.1 build 633:

    >perl -le "printf('%04d', '42a');" 0042
      My take on the docs is that '%4.4d' is legal. Maybe not the most straight forward way, but legal. I think I regularly abuse it this way, a mental hangover from the 's' format I think.

      Anyway, no matter, '%04d' doesn't numify here either.

      Yuck%4.4d

      Update: From perldoc -f sprintf:

      Perl permits the following universally-known flags between the "%" and the conversion letter: space prefix positive number with a space + prefix positive number with a plus sign - left-justify within the field 0 use zeros, not spaces, to right-justify # prefix non-zero octal with "0", non-zero hex with "0x" number minimum field width .number "precision": digits after decimal point for floating-point, max length for string, minimum length for integer
Re: printf numification
by hv (Prior) on Sep 27, 2004 at 21:42 UTC

    It appears that this was a bug in perl-5.6.0 that caused this to fail, but it was fixed in time for 5.6.1. Checking other versions I have here, it gives the expected answer:

    zen% perl -we 'printf "<%4.4d>\n", "42a"' Argument "42a" isn't numeric in printf at -e line 1. <0042> zen%
    for all versions in qw/ 5.004 5.004_05 5.005_03 5.6.1 5.8.* /, and the wrong answer:
    zen% /opt/perl-5.6.0/bin/perl -we 'printf "<%4.4d>\n", "42a"' Argument "42a" isn't numeric in printf at -e line 1. <0000> zen%
    only for 5.6.0.

    Hugo

Re: printf numification
by ambrus (Abbot) on Sep 27, 2004 at 19:11 UTC

    I've run the code you've given in perl 5.8.5, and I've got what I've expected anyway:

    '%d' output : 0042 '%d' output (with +0): 0042 '%f' output : 0042
    so %d does numericize for me.

    Also you might want to try "%04d" instead of "%4.4d" which should have the same or similar effect.