my @host = qw[ 192.168.15.2 192.168.16.2 192.168.17.2 192.168.18.2 ]; my $stout; my $counter = 0; while ($stout = run($host[$counter])) { last unless $stout == 256; if ($counter < $@host) { print "$host[$counter] not responding, trying $host[$counter+1]\n"; $counter++; } else { print "$host[$counter] not responding, no more hosts to try.\n"; last; } }