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" +); }
IPADDRESS alias1 alias2 alias5 IPADDRESS2 alias3 alias4 IPADDRESS2 alias3 alias5 IPADDRESS2 alias3 alias5
Hope this helps!IPADDRESS alias1 alias2 alias5 IPADDRESS2 alias3 alias4 alias5
In reply to Re^2: Perl and arrays of arrays?
by Koolstylez
in thread Perl and arrays of arrays?
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |