Perl code help

i'm using the below perl code to get a list of ips:

my @allipsfound ;
for my $elem (@gatches) {
    my ($ip) = split ":",$elem;
    print "$ip \n";
}

it spits out several lines of ips. most of those ips are the same. how do i do sort and uniq in the above code to make it so that it only spits out an IP once, instead of several instances of the IP?

Please post a sample of the content of the array @gatches.

Hi,,

Try something like this..

my @allipsfound ;
my %ips = ();
for my $elem (@gatches) {
    my ($ip) = split ":",$elem;
    $ips{$ip} = 1;
}
print join('\n', sort keys %ips), "\n";

1 Like

this worked. thank you so much!