in reply to printing array and string on same line
When you assign lines of input to "@list", this tells perl to read until the "end of file", which in the case of stdin means "until user hits '^D' (on a unix system) or '^Z' (on a windows/dos system)";@list = <>; ... $number = <>; <strike># this will never be reached (or won't read any +thing)</strike>
It would be better -- both for the program logic, and for the user, too, I think -- to structure the program so that it reads the list from some file; the user may provide the file name as a command line argument, or, if you insist, he can enter the file name in response to a prompt from the script. In either case, the script gets the file name in a scalar variable, you open that file, and read its contents into @list; e.g.:
open(LIST, "<$ARGV[0]"); @list = <LIST>; close LIST;
Then you prompt the user for a line number, and do what you want with that information. (Might be helpful, when prompting for that, to indicate how many lines are available to choose from.)
print "Pick a number between 1 and ",scalar @list,": "; $line_no = <STDIN>; chomp $line_no; print "Contents of line # $line_no in $ARGV[0]:\n$list[$line_no-1]\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: printing array and string on same line
by sauoq (Abbot) on Jun 11, 2003 at 07:08 UTC | |
|
Re: Re: printing array and string on same line
by BigRare (Pilgrim) on Jun 11, 2003 at 18:08 UTC |