Hi there, just wondered if somebody could help me with a problem I have
I have a program that when run from the command line will output a list of objects
# list_servers
server1
server5
server7
server8
#
I just wanted to know, in perl, how can i make each line of output from the above coimmand, a selectable option in an interactive perl script, so for example
# example.pl
Please select from the following available servers
1) server1
2) server5
3) server7
4) server8
enter selection:
Thank you for responding, however, it wasnt so much the capture of the input (ive already got that bit written, but thanks anyway ) but how to turn the output of a command 'dynamically' into a list
for example, the the output of the list_servers script I mentioned before is variable, and could produce a different list of servers each time.
Sorry for not paying attention to your question.
Here's an idea to implement a dynamic list -
$
$
$ # run the program that prints list of servers
$ ./list_servers
server1
server5
server7
server8
$
$ #
$ # display the content of the Perl program
$ cat -n example.pl
1 #perl -w
2
3 # fill up the server list
4 @servers = `./list_servers`;
5 $num = 0;
6
7 # print the main menu
8 print "Please select from the following available servers\n\n";
9 foreach $i (@servers) {
10 print ++$num,") $i";
11 }
12
13 # prompt for user's choice
14 printf("\n%s", "Enter selection: ");
15
16 # capture the choice
17 $choice = <STDIN>;
18
19 # and finally print it
20 print "You entered : ",$choice;
21 print "So you chose : ",$servers[$choice-1];
22
$
$ # now run the Perl program
$ perl example.pl
Please select from the following available servers
1) server1
2) server5
3) server7
4) server8
Enter selection: 3
You entered : 3
So you chose : server7
$
$
HTH,
tyler_durden
I haven't added any code for input validation though.