use strict; my %exception_second_octet = map {$_=> "Put Subclass Info here"} qw( 6 17 19); for my $second_octet (1..23){ if ($exception_second_octet{$second_octet}){ # Put Subclass generation code here } else { for my $fourth_octet(1..255){ my $generated_ip = "10.$second_octet.0.$fourth_octet/32"; # Do whatever else ... } } }