in reply to Adding a numeric key to list items and creating a user selection list
What I get now is the index that starts at 0 and that's not what I want.
Obvious solution: add 1 to each number.
my @vdbhosts = sort @lines; foreach my $i (0..$#vdbhosts) { print " " . $i + 1 . ". $vdbhosts[$i]\n"; } print "\n";
Then when you've retrieved the number from the user simply subtract 1 to obtain the index in the array. eg:
my ($userinput) = (<> =~ /([0-9]+)/g); die "User did not input a number" unless defined $userinput; die "User's number ($userinput) is out of range" unless (0 < $userinpu +t and $#vdbhosts >= $userinput - 1); print "Your choice was number $userinput which is " . $vdbhosts[$useri +nput - 1] . "\n";
Or is there some deeper problem which I've missed?
Expert's note: You can change the starting index of an array to be 1 instead of 0. But don't do that unless/until you know what you're doing.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Adding a numeric key to list items and creating a user selection list
by stevek1974 (Novice) on Sep 25, 2015 at 18:49 UTC | |
by hippo (Archbishop) on Sep 25, 2015 at 21:42 UTC | |
by stevek1974 (Novice) on Sep 25, 2015 at 22:06 UTC | |
by hippo (Archbishop) on Sep 26, 2015 at 09:02 UTC | |
by stevek1974 (Novice) on Sep 26, 2015 at 16:28 UTC | |
by stevek1974 (Novice) on Sep 28, 2015 at 22:21 UTC |