use Socket; my $mask = 24; # 255.255.255.0 -> 24 bit netmask my $network = inet_aton("192.168.20.0"); my @myips; foreach $ip ( @iplist ) { $ip = inet_ntoa($ip); # convert ascii to decimal if( ($ip & $mask) == ($network & $mask) ){ push @myips, $ip; } } # this will sort the list properly since it's sorting the numbers. # otherwise it would sort 192.168.20.20 before 196.168.20.3 @myips = sort @myips; foreach $ip ( @myips ) { # print out the ascii format print inet_ntoa($ip), "\n"; }