in reply to IP Address consolidation

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:

Replies are listed 'Best First'.
Re: Re: IP Address consolidation
by yasysad (Novice) on Aug 20, 2001 at 16:58 UTC
    I tried your code and got this error at this line
    $start = inet_ntoa($start);
    the error output is :
    Bad arg length for Socket::inet_ntoa, length is 10, should be 4 at ipconsnew.pl line 16, <INPUT> line 1.

    I am working on ActivePerl on Windows NT .. Am I doing anything wrong ??
Re: Re: IP Address consolidation
by yasysad (Novice) on Aug 20, 2001 at 15:03 UTC
    Thanks tadman .. the functions are a revelation .. I may be able to work on them for the resolve conflict ..
    actually, it's the algorithm I was looking for ..