my $HostsPerCluster{$clusterNumber} = scalar(@clusterHosts); my $totalClusterHosts = $HostsPerCluster{$clusterNumber} my $failures = 0; my $host_7 = $clusterHosts[-1]; ### this sub is called in a looping statement &do_stuff( $host, $file); if ($totalClusterHosts - $failures < 2) { ### exit this loop and do other stuff } sub do_stuff { my $command = "/bin/true"; &sshExec('SESSION', $command); $? && ++$failures; return $success; }