foreach my $host_num ( 0..num_hosts()-1 ) { print "[$host_num] ", hostname_by_num($host_num), "\n" ; } #### use Scalar::Util qw(looks_like_number); sub hostname_by_num { my $num = shift; # validate our argument defined $num or return undef; $num >= 1 or return undef; looks_like_number( $num ) or return undef; $num--; # convert into array index. return exists $HOSTS[$num] ? $HOSTS[$num] : undef; } #### sub choose_host { my $chosen_host; while ( !defined $chosen_host ) { printchoices(); $chosen_host = hostname_by_num( getchoice() ); } return $chosen_host; } # for loop can now be: sub printchoices { print join "\n", "\n", "ssh hosts", "-"x8; foreach my $host_num ( 1..num_hosts() ) { print "[$host_num] ", hostname_by_num($host_num), "\n" ; } } #### sub ssh_string { my $host = shift; return join '', username(), '@', $host, domain(); }