my $net = "192.168.4."; ... for my $counter ( 10 .. 14 ) { my $ip = $net . $counter; ... open(LOGF,">>",$logpath . $ip); ... while ( ) { ... } } }