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

Re: Challenge Problem: Merging Network Addresses

by merlyn (Sage)
on Oct 12, 2001 at 01:14 UTC ( #118350=note: print w/ replies, xml ) Need Help??


in reply to Challenge Problem: Merging Network Addresses

Use Net::CIDR's cidr2range, sort the ranges, combine the ones that are overlapping or adjacent, then use range2cidr to go back.

-- Randal L. Schwartz, Perl hacker


update Shoot, I totally missed cidradd in the same module, which does precisely that!
$ perl use Net::CIDR qw(cidradd); print join (", ", cidradd(qw{ 209.152.214.112/30 209.152.214.116/31 209.152.214.118/31 })), "\n" ^D 209.152.214.112/29 $ perl use Net::CIDR qw(cidradd); print join (", ", cidradd(qw{ 209.152.214.112/31 209.152.214.116/31 209.152.214.118/31 })), "\n" ^D 209.152.214.112/31, 209.152.214.116/30


Comment on Re: Challenge Problem: Merging Network Addresses
Download Code
(tye)Re: Challenge Problem: Merging Network Addresses
by tye (Cardinal) on Oct 12, 2001 at 01:45 UTC
Re: Challenge Problem: Merging Network Addresses
by Dominus (Parson) on Oct 12, 2001 at 02:25 UTC
    That's a great answer. Thanks a lot.

    I can believe that Net::CIDR is the fastest way to do this in Perl. Unfortunately, it may be too slow. My test program took about 2.5 minutes to process test file nm5.in. The C program is about 120-150 times faster.

    That's a bummer. I was hoping that there would be a Perl solution that would be fast enough to be useful.

    --
    Mark Dominus
    Perl Paraphernalia

      I was hoping that there would be a Perl solution that would be fast enough to be useful

      Of course if you patched the module with XS and your C code there would be :-) Just a random thought to increase your workload for the benefit of everyone (except perhaps you).

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

        Says tachyon:
        Of course if you patched the module with XS and your C code there would be :-)
        Not really. The C program is a complete program that solves this one specific problem. There's nowhere in the Net::CIDR interface to 'patch'; the module doesn't do what this program does, and this program doesn't implement any of the functions that Net::CIDR does.

        Even if I did put it in, there wouldn't be any benefit. At present, you could compile my program and then use:

        #!/usr/bin/perl system("mjd-merge-blocks inputfile > outputfile");
        Then you could call it a "Perl solution", but it wouldn't really be one. Hacking my code into Net::CIDR wouldn't be a "Perl solution" either.

        --
        Mark Dominus
        Perl Paraphernalia

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://118350]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2014-12-29 04:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (184 votes), past polls