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

Good afternoon, I have a list of IP addresses ranges that is very long. I see that many of them can be combined to shorten list. Can you help with some perl code that will do this quickly? Example: File In contains:
5.0.0.0-5.0.0.255 5.0.1.0-5.0.1.255 7.15.1.0-7.15.6.255 7.15.7.0-7.15.9.255
Collapsed output file:
5.0.0.0-5.0.1.255 7.15.1.0-7.15.9.255
Thank you.

Replies are listed 'Best First'.
Re: Collapse IP Ranges
by runrig (Abbot) on Apr 14, 2009 at 23:21 UTC
    Net::CIDR::Lite does this quite easily:
    my $cidr = Net::CIDR::Lite->new(); while (<DATA>) { chomp; $cidr->add_range($_); } for $r ( $cidr->list_range() ) { print "$r\n"; } __DATA__ 5.0.0.0-5.0.0.255 5.0.1.0-5.0.1.255 7.15.1.0-7.15.6.255 7.15.7.0-7.15.9.255
      Works kick ass! Thanks.
Re: Collapse IP Ranges
by toolic (Bishop) on Apr 14, 2009 at 23:33 UTC
      Ironically, that is a node where I do not recommend the module for that particular problem (but at least mention it so the OP here could have at least looked into it).
      Also, if the OP googled for the right stuff (or even some other stuff) he could have also found help (though it seems the word "merge" is much more helpful than "collapse").
      That post is about identifying the subnet address of an IP address given a subnet mask. I don't see how that relates.