Small function to strip padding from numeric IP addresses. For example 192.168.010.001 becomes 192.168.10.1.
sub normalize_ip{ return join(".", map {sprintf "%d", $_} split(/\./,shift)); }

Replies are listed 'Best First'.
Re: Removing padding from numeric IP addresses
by AltBlue (Chaplain) on Jan 27, 2001 at 05:00 UTC
    wouldn't s/(\.|^)0+/$1/g do the same thing ? 8-{Q}

    update: hehe, lemming had a good point in CB, i forgot about the ^ :) corrected.

    update2: coyotte also gets a good point in his reply, so here is another update :)

    s/(\.|^)0{1,2}/$1/g

    update3: and still, our great tye discovered another bug :) and yes, his solution is ofc good (s/(\.|^)\b0+\B/$1/g). ah, for the record, ofc that i like coyotte's solution 2, is the nicest :)

    --
    AltBlue.

      It does until you run into an address like 192.168.010.000. Which ends up as 192.168.10. after you run it through the regex above.

      ----
      Coyote

      Similar problem for update2, 1.2.3.0 doesn't work. I think that s/\b0+\B//g whould work. But I kinda like the original solution as it is easy to read and it is easy to code it correctly.

              - tye (but my friends call me "Tye")
Re: Removing padding from numeric IP addresses
by Anonymous Monk on Feb 17, 2001 at 04:16 UTC
    Ummm... "Padding" is not really a good thing to have in general for IP octets. On some operating systems the leading "0" could imply that the operating system interpret the octet to be an octal value. This being the case "010" would really be interpreted as an "8". This can have very bizzarre side effects for digits greater than 7. SunOS used to do it this way, and I would imagine other do as well. It may look "pretty, symetric, and sort nicely", but it is not good practice, IMHO.

      Actually, I don't think this is an operating system feature (when I've seen it, some programs in the operating system exhibited the behavior while others didn't). I think it is just C coders using atol() with an argument of 0 instead of with an argument of 10. I personally consider such as a bug in said C code, but I've seen it enough that I encourage everyone to take your warning seriously.

      Update: Well, I would hope that this bug isn't present in the basic inet libraries on any operating system...

              - tye (but my friends call me "Tye")