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

Ye Monks,
I'm trying to split a string (in this case an IP address) on the second period and assign it to $iprefix and $ipsuff using the split function and some regular expression voodoo.
$ipaddr = "192.168.8.205"; ($iprefix, $ipsuff) = split /\.{2}/,$ipaddr; # Split on 2nd . print($iprefix);
What I want:
$iprefix = "192.168"

What I get:
$iprefix = "192.168.8.205"

Any help would be greatly appreciated.

Replies are listed 'Best First'.
Re: Split on 2nd Occurence
by rob_au (Abbot) on Jan 11, 2002 at 18:20 UTC
    The easiest way that I can think of would be within a match rather than by splitting based upon a match pattern ...

    my $ipaddr = "192.168.8.205"; my ($iprefix, $ipstuff) = $ipaddr =~ m/(\d+\.\d+)\.(\d+\.\d+)/; print $iprefix, " ", $ipstuff, "\n";

     

    perl -e 's&&rob@cowsnet.com.au&&&split/[@.]/&&s&.com.&_&&&print'

Re: Split on 2nd Occurence
by dmmiller2k (Chaplain) on Jan 11, 2002 at 18:31 UTC
    The simplest way I can think of is:
    my ($iprefix, $ipsuff) = $ipaddr =~ /(\d+\.\d+)\.(\d+\.\d+)/;

    Update: sorry rob_au, you posted yours as I was typing this. Not intentional plagiarism... :)

    dmm

    If you GIVE a man a fish you feed him for a day
    But,
    TEACH him to fish and you feed him for a lifetime
      *chuckle* ... It's a pretty obvious solution so I would have been surprised if noone else came up with it - Think of the two of us as merely two monkeys at two typewriters breaking the statistical curve in a room of an infinite number of monkeys :-)

       

      perl -e 's&&rob@cowsnet.com.au&&&split/[@.]/&&s&.com.&_&&&print'

Re: Split on 2nd Occurence
by rdfield (Priest) on Jan 11, 2002 at 22:00 UTC
    Just to be different:

    my $ipaddr = "192.168.0.11"; my ($prefix) = join('.',@{[split(/\./,$ipaddr)]}[0,1]);
      Even this different approach would be clearer with a list slice:

      my $prefix = join '.', (split /\./, $ipaddr)[0,1];

          -- Chip Salzenberg, Free-Floating Agent of Chaos

      That seems like a lot of punctuation:
      my ($prefix) = join('.',@{[split(/\./,$ipaddr)]}[0,1]);
      Can also be written like this:
      my $prefix = join '.', (split /\./, $ipaddr)[0,1];
      Update: Oops.... much like the other "dup" in this thread this was wholly unintentional, and not blatent copying.

      -Blake

Re: Split on 2nd Occurence
by grep (Monsignor) on Jan 12, 2002 at 03:23 UTC
    These are all great answers, but I think we are pointing this AM in the wrong direction. He is obviously looking the the network address (which is not 192.168 it is 192.168.0.0). These solutions (not they they were not what the AM asked for) are not portable to anything but a Class B address and does not take into consideration CIDR or other netmasks (what if the netmask id 255.255.255.0 then 192.168.8.0 would the correct network for the problem). The module NetAddr-IP-3.07 will handle getting the network address and any other info the AM would want about a particular IP.
    #!/usr/bin/perl use warnings; use strict; use NetAddr::IP; my $ip = new NetAddr::IP ('192.168.8.205','255.255.0.0'); my $network = $ip->network; print $network;


    grep
    grep> cd pub grep> more beer
Re: Split on 2nd Occurence
by jonjacobmoon (Pilgrim) on Jan 11, 2002 at 18:16 UTC
    What you are doing here is spliting on ".." not "."

    My Solution is:

    $ipaddr = "192.168.8.205"; @ip = split /\./,$ipaddr; # Split on 2nd . $iprefix =$ip[0] . "." . $ip[1]; print($iprefix);

    There may be a better way, but its 5am, I had 3 hours sleep so I went with what I knew would work :)


    I admit it, I am Paco.
Re: Split on 2nd Occurence
by redsquirrel (Hermit) on Jan 11, 2002 at 19:01 UTC
    If you are only interested in printing the prefix, this works:
    $ipaddr = '192.168.8.205'; print ($ipaddr =~ /^(\d+\.\d+)/);
    --Dave