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

    I implemented the first portion of code and it did exactly what I wanted. I now have a list with all returned items ordered from 1 to X. I issue though. The first portion of the print line is supposed to indent the list by 2 spaces. The spaces are being ignored and instead the list is left justified to the edge of the screen.

    Second portion works as well, however I need to modify it a bit to get what I want. My goal is to allow for input of multiple numbers separated by commas which would then be printed to a text file with each entry being on a new line. Working on this part now.

    Thank you!

      The first portion of the print line is supposed to indent the list by 2 spaces. The spaces are being ignored and instead the list is left justified to the edge of the screen.

      There are plenty of options for formatted output in Perl. However, the most straight forward (particularly with those familiar with it from other languages) is likely to be printf.

        I figured that out and used printf to achieve the initial spacing. I also figured out how to accept the comma separated input, however I am having a small issue with this. When it renders the output on the screen, it is separating the numeric identifier from the data. So, what I get is:

        Enter Selection(s): 1,3 #this is what the user will input and the results output below

          1. 10.10.10.11
          3
        .10.10.10.13

        What I expect to see is this

          1. 10.10.10.11
          3. 10.10.10.13

        I found that adding a trailing comma to the input 1,3, resolved the issue, but created another warning

        Argument "\n" isn't numeric in subtraction (-)

        # Import cluster hosts list and print sorted output print color('bold white') . "The following hosts were found: \n\n" . c +olor('reset'); use File::Slurp; my @chosts = read_file($chosts_file); my @vdbhosts = sort @chosts; foreach my $i (0..$#vdbhosts ) { printf '%-2s',""; print $i + 1 . ". $vdbhosts[$i]"; } print "\n"; ASSIGN_VDB_HOSTS: # Choose the hosts from the list of above print "Please select the hosts from the list above separated by commas + (1,2,3...)\n"; print "\n"; print "Enter selection(s): "; my $vdbhostinput = (<>); print "\n"; my @vdbvalues = split(',', $vdbhostinput); foreach my $i (@vdbvalues){ print " " . $i . ". " . $vdbhosts[$i - 1]; } print "\n";