Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^3: Adding a numeric key to list items and creating a user selection list

by hippo (Bishop)
on Sep 25, 2015 at 21:42 UTC ( [id://1143037]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Adding a numeric key to list items and creating a user selection list
in thread Adding a numeric key to list items and creating a user selection list

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.

  • Comment on Re^3: Adding a numeric key to list items and creating a user selection list

Replies are listed 'Best First'.
Re^4: Adding a numeric key to list items and creating a user selection list
by stevek1974 (Novice) on Sep 25, 2015 at 22:06 UTC

    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";

        Works like a champ! Now, on to validation. Thanks again!

        Update

        Resolved

        Maybe not the best solution but I changed the check to...

        foreach my $i (@vdblist){ if ($i == 0){ print BOLD WHITE ON_RED, "Error: $i does not correspond to a h +ost in the list!", RESET; print "\n\n"; goto CLUSTER_HOSTS; } elsif ($i > scalar $#vdbhosts){ print BOLD WHITE ON_RED, "Error: $i does not correspond to a h +ost in the list!", RESET; print "\n\n"; goto CLUSTER_HOSTS; } }

        Now it works!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1143037]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (3)
As of 2024-04-19 05:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found