Here is what it looks like as a hash of arrays. It's not as scalable as a hash of hashes, since it needs to grep through the array of aliases each time, but on small files you shouldn't notice.
use strict; # Hash of arrays # USAGE: $masterhash{$ipaddress} = @aliases my %masterhash; my $input_file = "test.in"; my $output_file = "test.out"; open INPUT_FILE, "<$input_file" or die "Could not open $input_file\n"; # Loop through each line in the input file while(my $line = <INPUT_FILE>) { # Split line on spaces my @line_parts = split(/\s/, $line); # Grab the IP address my $ipaddress = shift @line_parts; # Loop through the aliases foreach my $alias (@line_parts) { # Search through the array for that IP address to determine # whether or not the current alias is already there # # The value in $masterhash{$ipaddress} is an array reference, so # we have to wrap it with a @{} to use it as an array unless(grep {$_ eq $alias} @{$masterhash{$ipaddress}}) { # If we get here, its a new alias for this IP address so add it push @{$masterhash{$ipaddress}}, $alias; } } } # Printing output... open OUTPUT_FILE, ">$output_file" or die "Could not open $output_file\ +n"; # Print one line per IP address foreach my $ip (sort keys %masterhash) { # Print the IP, followed by all of its aliases joined by spaces. print OUTPUT_FILE ("$ip ", join(" ", sort @{$masterhash{$ip}}), "\n" +); }

test.in
IPADDRESS alias1 alias2 alias5 IPADDRESS2 alias3 alias4 IPADDRESS2 alias3 alias5 IPADDRESS2 alias3 alias5

test.out
IPADDRESS alias1 alias2 alias5 IPADDRESS2 alias3 alias4 alias5
Hope this helps!

In reply to Re^2: Perl and arrays of arrays? by Koolstylez
in thread Perl and arrays of arrays? by Anonymous Monk

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.