in reply to Read two values from input

the issue in your code is 10 20 is going into $a only.

if you want to get the values in different variables directly


try this
#! usr/bin/perl -w use strict; print "enter two values\n"; my $x=<STDIN>; chomp($x); my $y=<STDIN>; chomp($y); print "two values are $x,$y\n";
output
enter two values 10 20 two values are 10,20
the first two lines of code are not compulsion here
see this

Replies are listed 'Best First'.
Re^2: Read two values from input
by davido (Cardinal) on Nov 25, 2011 at 05:23 UTC

    The original specification was to enter two numbers, "10 20", which seemed to be coming from the same line of input. Your solution works only if the two numbers come on separate lines of input, so it fails if the original spec. was accurate. Frankly, aside from chomp, you essentially presented the OP's solution back to him.

    Only one line of input is to be read, and must then be split to multiple components, as demonstrated in other solutions earlier in the thread. Depending on how you wield split, chomp may or may not also be necessary. But it's really not the pivotal component of a correct solution.

    You also linked to a page about using strict, which while being a "good idea", has nothing to do with why the OP's code wasn't doing what he expected.


    Dave

      davido
      i have written
      "if you want to get the values in different variables directly"
      and
      "the first two lines of code are not compulsion here"
      i think that explains that i know the user specifications
      and used warning and strict because i have been told by many monks to do so
      submitted the solution thinking that i would be beneficial for knils_r00t.
      sorry if i am wrong

        There is nothing wrong with using strict in your example, and there's no problem showing the shebang line either. As a matter of fact, I almost always use strict and warnings in example code I provide. But I usually don't make a big deal about it by mentioning it explicitly unless strict or warnings would have prevented the OP from running into trouble in his specific situation. It's one thing to demonstrate a good practice at work. It's another to confuse the intent of your solution with suggestions that are not related to the solution.

        My issues are these:

        • The original poster's example input showed two numbers on the same input line. If that is an accurate example (which we have to assume it is), your solution fails to work. Your solution will NOT end up putting two numbers into two different variables if both numbers are presented on one line of input. If you feel that your solution meets the OP's needs, you either still don't understand the question, or you don't understand your own code.
        • You directed the OP to review a node dedicated to using strictures, yet you gave no indication as to why you felt that is relevant to the problem (and in fact, it isn't). Consequently you sent the OP on a mission to read an irrelevant document. You might as well have suggested he read perlopentut; equally irrelevant to his problem. Perhaps you don't fully understand what 'strict' does. If you do understand, you might want to indicate in your post why you felt it's relevant.

        I think it's great that you're learning Perl and contributing to threads here. My advice is to be sure you understand the question, test the code in your responses, and do your best to keep your response relevant to the issue at hand.

        Please, by all means continue contributing. I'm trying to offer advice that will help you to improve the quality of your posts, and that will reduce the need for someone asking a question to have to distinguish between good advice and bad advice.

        Don't worry, we're all trying our best here to make Perl a great tool, and the Monastery a great place to learn to use it.


        Dave