stevek1974 has asked for the wisdom of the Perl Monks concerning the following question:
Update
I've made progress thanks to the input here. I'm having a little trouble with the error handling. Basically what I want is for the error checking to see if the input is gt or lt the array index or = to 0. It works great unless a value input is gt an index. When greater, I get:
Use of uninitialized value within @vdbhosts in regexp compilation at ./deploy-beta line 129, <> line 2.
This corresponds to this line of code
if (($i =~ $vdbhosts[$i - 1])||($i == 0)){
Here is the full code
# Get count of hosts found for the specified cluster my $chostcounter = 0; foreach my $chost (@$host_views) { $chostcounter++; } # Check that hosts exist, display error and re-direct to CLUSTER: if ($chostcounter == 0){ print "\n"; print BOLD WHITE ON_RED, "Error: No hosts were found in the " . $c +luster_name . " cluster!\n", RESET; goto CLUSTER; } # Export cluster hosts list to a file my $chosts_file = '/root/deploypoc/export/cluster_hosts.txt'; open (CHOSTSFILE, '>', $chosts_file) or die " Problem opening $chosts_ +file\n"; foreach my $chost (@$host_views) { print CHOSTSFILE $chost->{'name'}, "\n"; } close CHOSTSFILE; # VDB host selection print BOLD WHITE ON_BLUE, " Host Selection: The following hosts were found for the + \n" . " specified cluster. Please select the number(s)that + \n" . " correspond to the desired host(s). If entering more than + \n" . " one (1), separate each entry with a comma (1,2,3...) + \n", RESET; print "\n"; # Import cluster hosts list and print sorted output use File::Slurp; my @chosts = read_file($chosts_file); my @vdbhosts = sort @chosts; CLUSTER_HOSTS: foreach my $i (0..$#vdbhosts ) { printf '%-2s',""; print $i + 1 . ". $vdbhosts[$i]"; } print "\n"; ASSIGN_VDB_HOSTS: # Select the hosts from the list above print "Enter selection(s): "; my $vdbhostinput = (<>); chomp $vdbhostinput; print "\n"; my @vdblist = split(',', $vdbhostinput); print "The following hosts were selected:\n\n"; foreach my $i (@vdblist){ # Check to ensure user input is valid and display selected hosts if (($i != $vdbhosts[$i - 1])||($i == 0)){ print BOLD WHITE ON_RED; print "Error: $i does not correspond to a host in the list!"; print RESET; print "\n\n"; goto CLUSTER_HOSTS; } print " " . $i . ". " . $vdbhosts[$i - 1]; }
|
|---|