#! perl use strict; use warnings; use diagnostics; use Data::Dumper; my $destination = '92.123.72.112'; my $final_ip = '92.123.72.0'; # Get the first 3 octets of each IP my $destn_prefix = $destination =~ s/ \. \d{1,3} $ //rx; my $final_prefix = $final_ip =~ s/ \. \d{1,3} $ //rx; # Populate the hash my %hash; if ($destn_prefix eq $final_prefix) { $hash{$destn_prefix} = [ $destination, $final_ip ]; } print Dumper(\%hash), "\n"; # Access a given location my $ip = '92.123.72'; if (exists $hash{$ip}) { my @ips = @{ $hash{$ip} }; print 'IPs at location ', $ip, ' are: ', join(', ', @ips), "\n"; } #### $VAR1 = { '92.123.72' => [ '92.123.72.112', '92.123.72.0' ] }; IPs at location 92.123.72 are: 92.123.72.112, 92.123.72.0