in reply to decrementing numbers

Do you need to handle underflow (when the last octet is 0)? If so, would it just wrap to 255, or would you decrement one from the previous octet?

If you need to do the second, one way to solve this is to convert the ipv4 address into a 32 bit number, subtract one, and then convert back. There are network-address functions (I cannot remember or find them right now, grr) for doing this, but pack / unpack is also an option for this.

It is said that "only perl can parse Perl." I don't even come close until my 3rd cup of coffee. --MidLifeXis

Replies are listed 'Best First'.
Re^2: decrementing numbers
by almut (Canon) on Mar 05, 2010 at 18:54 UTC
    There are network-address functions...
    use Socket; my $n32 = inet_aton("192.168.254.0"); print inet_ntoa(pack("N",unpack("N",$n32)-1)); # "192.168.253.255"

    Or, in the OP's context:

    s/(\d+\.\d+\.\d+\.\d+)/inet_ntoa(pack("N",unpack("N",inet_aton($1))-1) +)/e;

    But (to quote the docs): "For portability do not assume that the result of inet_aton() is 32 bits wide, in other words, that it would contain only the IPv4 address in network order."

Re^2: decrementing numbers
by ddrew78 (Beadle) on Mar 05, 2010 at 20:23 UTC
    The last octet will never be "0". These WAN IP's, running off /30's. But thanks for considering it. I'm trying to work with some of the fixes in here, but can't get any of them to work for me. Guess I'll keep hacking away at it :-( Thanks to everyone for their help.
      ...but can't get any of them to work for me.

      If you let us in on what problems you have, we might be able to assist...   In other words, what exactly have you tried, and how did it fail?