If you have the capacity, which you should, it would be fairly straightfoward to load all the files into memory, and then write them out. Since these are grouped by name, why not use a Hash of Arrays (HoA):
my %data; foreach my $file (@file_list) { open (INPUT, $file) || warn "Could not read $file\n"; while (<INPUT>) { chomp; my ($start,$end,$name) = split (/,/); push (@{$data{$name}}, "$start,$end"); } close (INPUT); } foreach (sort keys %data) { print "@{$data{$_}},$_\n"; }
If you have overlapping entries in the different files, then you will have to check on insert. This could be done with a Hash of Hashes (HoH):
use Socket; my %data; foreach my $file (@file_list) { open (INPUT, $file) || warn "Could not read $file\n"; while (<INPUT>) { chomp; my ($start,$end,$name) = split (/,/); $start = inet_aton($start); $end = inet_aton($end); if (defined $data{$name}{$start}) { # Resolve conflict? } else { $data{$name}{$start} = $end; } } close (INPUT); } foreach my $name (sort keys %data) { foreach my $start (sort keys %{$data{$name}}) { print join (',', inet_ntoa($start), inet_ntoa($end), $name), "\n"; } }
The reason for using inet_aton (ASCII to Number) from the Socket module is to simplify comparisons. "202.1.2.0" and "202.01.002.0" are equivalent, and removing redundant zeros is a lot more complicated than just "packing" them into their native format (4 bytes). They are easily unpacked with the complementary inet_ntoa (Number to ASCII), and should always come out clean with no extraneous zeros.

Additionally, if you want to sort them, which I'm doing here with the regular sort operator, they will sort ASCII-betically, which should put them in order. Numeric sorts are more complicated, especially those with multiple points.

Update:

In reply to Re: IP Address consolidation by tadman
in thread IP Address consolidation by yasysad

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.