If there is a better solution that uses established modules, I would suggest that. Barring that, here is some code I was toying with a while back to do something similar-probably not the best or most efficient way to do it, however. (Address data was in __DATA__, which I did not include here for length.) Hope maybe this might give you an idea or two (if nothing else, maybe of how not to do it).

#!/usr/bin/perl -w use strict; my (%addresses); { # Load data into @addresslist, # ensuring only unique addresses are present my @addresslist = (); { my (%seen); while (<DATA>) { chomp; my $ip = unpack( "N", pack( "C4", split ( /\D+/, $_ ) ) ); $seen{ $ip }++; } @addresslist = keys(%seen); } # Look thru @addresslist, # storing in %addresses if there is not a key # where $flag <= $i <= $addresses{$flag}, or # incrementing $addresses{$flag} if $i == $addresses{$flag}+1 foreach my $i (@addresslist) { my $flag = 0; foreach my $k ( sort( keys(%addresses) ) ) { my $target = $addresses{$k} + 1; if ( $i == $target ) { $flag = $k; last; } } if ($flag) { $addresses{$flag}++; } else { $addresses{$i} = $i; } print "\n"; } # Join ranges that should have been connected, # but were not because data was not seen in sequence. my @startvalues = sort( keys(%addresses) ); my $i = shift (@startvalues); while (@startvalues) { my $k = shift (@startvalues); if ( $k == $addresses{$i} ) { $addresses{$i} = $addresses{$k}; delete( $addresses{$k} ); } else { $i = $k; } } } # Print address ranges on one line. foreach my $k ( sort( keys(%addresses) ) ) { print join ( '.', unpack( "C4", pack( "N", $k ) ) ); if ( $k != $addresses{$k} ) { print '-'; print join ( '.', unpack( "C4", pack( "N", $addresses{$k} ) ) ); } print " "; } print "\n";

In reply to Re: Unsorted IP Addr list to sorted IP list with ranges by atcroft
in thread Unsorted IP Addr list to sorted IP list with ranges by blue_cowdawg

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.