in reply to printing array and string on same line

You have a serious something that could be a misconception here:
@list = <>; ... $number = <>; <strike># this will never be reached (or won't read any +thing)</strike>
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)"; then of course, having terminated the input to the script on stdin, perl cannot read anything more. if the user doesn't know the proper etiquette for terminating manual input on stdin, this script will seem uncooperative. (Thanks to sauoq for reminding me about how perl really handles the <> input operator and end-of-file signals in that context.)

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
    having terminated the input to the script on stdin, perl cannot read anything more.

    Sorry, wrong answer.

    Quoting from perldoc perlop:

    The <> symbol will return "undef" for end-of-file only once. If you call it again after this, it will assume you are processing another @ARGV list, and if you haven't set @ARGV, will read input from STDIN.

    You can give it an end-of-file as many times as you want. If you keep going back and using <> afterwards, it'll read from stdin again. But don't take my word for it; just give it a whirl...

    perl -e 'while(1){ print for <> }'
    Type a few lines, ^D, wash, rinse, repeat.

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: Re: printing array and string on same line
by BigRare (Pilgrim) on Jun 11, 2003 at 18:08 UTC
    Thank you for your input.

    Two things.

    1)There is no challenge with EOF as I'm the only one running the script.

    2)The challenge is with the final print statement and why it does not print as expected.

    Other than that, thank you very much for the information.

    I know this is a very simple script, but it is just an example as to describe the problem I have been experiencing with the print function.