Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

printing array and string on same line

by BigRare (Pilgrim)
on Jun 11, 2003 at 00:06 UTC ( [id://264910]=perlquestion: print w/replies, xml ) Need Help??

BigRare has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to print an Array and a String in the same instance, but it does not work.

The code I'm using is:
#!/usr/bin/perl -w # This script should ask for a list, a number, then display # the line entered of the list in reference to the number print "Hello, please enter a list:/n"; @list=<>; chomp @list; print "Now, enter a number:/n"; $number=<>; chomp $number; # OK, now print the results print "Line $number was: $list[$number-1]";
I know this does not work, but I do not know why.

Could the reson for the non-workage be because I'm stuck here at work using ActivePerl on Windows or is the reason simply that this cannot be done?

Any suggestions or comments are welcome.

Replies are listed 'Best First'.
Re: printing array and string on same line
by sauoq (Abbot) on Jun 11, 2003 at 00:11 UTC

    That should work fine. You probably want to use \n rather than /n in your strings but I don't see anything that would break it. What problem are you having with it exactly?

    One caveat: I'm not sure how to do it on win (^Z, I think) but you'd need to type an end of file after you enter your list in (one element to a line.)

    -sauoq
    "My two cents aren't worth a dime.";
    
      Whoops, I am using \n rather than /n. Haha. Long day.

      What happens is that nothing prints. Odly enough if I don't chomp the array, it prints: was: $list[$number-1] with the correct values, but nothing else in the print statement.

      Update: Fixed tags in post.

        That sounds like you are chopping $number rather than chomping, and getting the carriage return printed after the number without the linefeed.

        In any case, to get it to print, it sounds like you might need to put a newline ("\n") on the end of your final print. (I say that because you are at least getting something if you don't chomp the array...)

        -sauoq
        "My two cents aren't worth a dime.";
        
Re: printing array and string on same line
by graff (Chancellor) on Jun 11, 2003 at 06:47 UTC
    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";
      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.";
      
      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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-18 04:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found