Monolith-0 has asked for the wisdom of the Perl Monks concerning the following question:

This, I'm sure, is just a simple one-line answer, but here's what I want to know how to do:
I want to take an IP address (for example) and add leading zeros to each of the numbers so that they are all three digits long.

ex.
1.23.456.78 -> 001.023.456.078

That's it.

Replies are listed 'Best First'.
Re: Leading Zeros
by dfog (Scribe) on Feb 22, 2001 at 10:17 UTC
    Quick and dirty code, as I seem to have a better way on the tip of my brain that I can't quite recall. This should help for now.
    my $x = "1.10.9.2"; my $y = sprintf ("%03d.%03d.%03d.%03d", split (/\./, $x));
Re: Leading Zeros
by Albannach (Monsignor) on Feb 22, 2001 at 10:23 UTC
    How about:
    $foo = '1.23.456.78'; print join '.', map {sprintf("%03d", $_)} split(/\./, $foo);

    Update: I was about to post a patch but tye beat me to it, so I'll just explain what it does. If you don't use the zero-pad in sprintf() then you get leading spaces, so you can substitute any desired character(s) for the spaces, and grep() works well for that. The code above becomes: print join '.', grep{s/ /-/g} map {sprintf("%03d", $_)} split(/\./, $foo);

    --
    I'd like to be able to assign to an luser

Re (tilly) 1: Leading Zeros
by tilly (Archbishop) on Feb 22, 2001 at 17:24 UTC
    s/(^|\.)(\d{1,3})/$1.("0"x(3-length($2))).$2/eg
      Here's some print scalar reverse "flog":
      s/\b(?=(\d\d?\b))/0 x(3-length$1)/eg; # or s/\b(?=(\d{1,3}))/0 x(3-length$1)/eg;


      japhy -- Perl and Regex Hacker
Re: Leading Zeros
by unixwzrd (Beadle) on Feb 22, 2001 at 13:26 UTC
    I mentioned in a post, while anonymous, about removing padding Re: Removing padding from numeric IP addresses that it was probably something to be avoided since I remember versions of SunOS interpreting them as octal.

    I think it was a problem in their IP code. It was a long time ago and probably fixed i.e. 1988?

    Mike
    "The two most common elements in the universe are hydrogen... and stupidity."
    Harlan Ellison
Re: Leading Zeros
by I0 (Priest) on Feb 23, 2001 at 06:22 UTC
    s/(\d+)/sprintf"%03d",$1/eg;or
    s/(\d+)/substr"00$1",-3/eg;
Re: Leading Zeros
by Monolith-0 (Beadle) on Feb 22, 2001 at 10:54 UTC
    Hmm.. ok. This'll do for what I need right now, but I was sort of looking for a way to use any sort of leading symbol, such as a space or dash. I'm pretty sure you can't do that with sprintf.

    Thanks for those two answers anyway.
      sub pad { my( $str, $width, $delim, $pad )= @_; $width= 3 if ! defined $width; $delim= "." if ! defined $delim; $pad= "0" if ! defined $pad; return join $delim, grep {s/ /$pad/g;1} map {sprintf"%${width}s",$_} split /\Q$delim\E/, $str, -1; }

      Use a negative width to pad in the other direction.

      Update: Fixed a minor bug (my abuse of grep should have ended with ";1" not ";$_") and wanted to note that the transformation of " " to $pad when it is part of the original string but not part of a delimiter is a feature. ;)

              - tye (but my friends call me "Tye")
        Thanks. That's just what I was looking for.
Re: Leading Zeros
by jaymoo (Novice) on Feb 22, 2001 at 22:12 UTC
    Well with a little help from kel I was able to piece this together.

    s/(\.|^)0+/$1/g

    Moo

      DOH! Well I guess I skimmed this one a little too quick. But the obove regex will work nice if you want to convert back to no leading zeros.

      Moo