Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Increment of ip address

by mosh (Scribe)
on Jun 16, 2005 at 15:52 UTC ( #467330=perlquestion: print w/replies, xml ) Need Help??
mosh has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks !

I tried to increment ip address as follows.
my $ip = '' +1;
I meant that the $ip would get the value ''.
Of course it wasn't work:
"Argument "" isn't numeric in addition (+)..."

Do anyone have an idea how should I do it ?


Replies are listed 'Best First'.
Re: Increment of ip address
by davidrw (Prior) on Jun 16, 2005 at 15:57 UTC
    Try Net::IP .. note especially that it overloads the ++ operator
      There's also NetAddr::IP which also overloads ++ (and + 1)
Re: Increment of ip address
by jdporter (Canon) on Jun 16, 2005 at 16:19 UTC
    Net::IP or NetAddr::IP is really the way to go... but for fun, here's a simple IP math function. (Note that it only works on IPv4 addresses.)
    use Socket; sub add_to_ip($$) { my( $ip, $add ) = @_; inet_ntoa pack( 'N', unpack('N',inet_aton $ip) + $add ) }
    You can add or subtract any amount to an IP address this way. Carry-over into the next "quad" is automatic... but that may not be what you want. :-)
    $j = add_to_ip( $i, 1 ); # simple increment $j = add_to_ip( $i, -1 ); # simple decrement $j = add_to_ip( $i, 300 ); # addition carries over into the next-to-la +st quad.
      Hi jdporter, Thanks !

      What does it mean the $$ in:
      sub add_to_ip($$)


        That would be a prototype, the subject of much discussion on usefulness verging on holy war status.  The Camel Book says the intent behind prototypes "... is primarily to let you define subroutines that work like built-in functions ..." (p. 226).  In this case, it is giving a contextual clue to perl that this sub accepts two scalars.

Re: Increment of ip address
by cool_jr256 (Acolyte) on Jun 16, 2005 at 16:02 UTC
    '' is being treated as a string (because of the '.'). You can split up the IP into an array, hence being able to increment any of the ip numbers:
    $ip=""; @ipnum=split(/./,$ip); $ipnum[$#ipnum]++; # this increments the last digit.. # then $ip=join(".",@ipnum); # result is '';
      Make sure that you split on /\./ and not just 'dot', which is any character. Also, if you increment with that method you're not going to get what you want ... (see other responses for modules to do it for you)
Re: Increment of ip address
by monkey_boy (Priest) on Jun 16, 2005 at 16:10 UTC
    As it has been mentioned , this does not work in many situations, please ignore my stupid ramblings .. the simple way ..
    use strict; my $ip = ''; my @ip = split(/\./,$ip); $ip[3]++; print join('.',@ip) , "\n";

    This is not a Signature...
Re: Increment of ip address
by monarch (Priest) on Jun 17, 2005 at 00:54 UTC
    In thread perl ipcalc oneliners I posted some quick math subroutines you can use for converting your IP address into a number and back for arithmetic purposes without relying on any other module.

    Try using:

    sub addrtoint { return( unpack( "N", pack( "C4", split( /[.]/,$_[0] ) ) ) ) }; sub inttoaddr { return( join( ".", unpack( "C4", pack( "N", $_[0] ) ) ) ) }; my $ipinc = addrtoint( '' ); $ipinc++; print( inttoaddr( $ipinc ) . "\n" );

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://467330]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (11)
As of 2017-07-26 09:23 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (387 votes). Check out past polls.