in reply to Re: Simple Arithmetic Question
in thread Simple Arithmetic Question

This is what I have so far I'm not sure how to use the chomp exactly so I'm sure that is wrong. It keeps showing a Global symbol "c" requires explicit package name in lines 14 and 17. I'm sure there are probably multiple errors in the code but I can't figure it out. If you have any pointers that would be great thanks!!

#!/usr/bin/perl use Modern::Perl; use strict; use warnings; print "input three numbers separated by spaces\n"; my $user_input = <>; chomp $user_input; my ($nr1) = $user_input, "$a"; chomp $user_input; my ($nr2) = $user_input, "$b"; chomp $user_input; my ($nr3) = $user_input, "$c"; print "The sum of the three numbers is equal to", ($a+$b+$c),"\n";

Replies are listed 'Best First'.
Re^3: Simple Arithmetic Question
by AnomalousMonk (Archbishop) on Nov 06, 2014 at 04:24 UTC
    print "input three numbers separated by spaces\n"; my $user_input = <>;

    This is good as far as it goes: you're getting something | a string from  STDIN that is terminated by a newline.

    chomp $user_input;

    This is good too, except you do it twice more and this is where you start to go off the rails. chomp removes a newline (if present) from the end of a string (well, it's a little more involved than that, but let it stand for now), $user_input in this case, so doing it repeatedly will not usually get you anything. For learning/development/debugging, I would advise placing a print statement after this step so you can be sure exactly what you are dealing with:
        print "A: '$user_unput' \n";
    (In general, develop your code step-by-step and use print statements at each successive step to see just what is happening. If weird stuff starts to happen, understand the problem before proceeding.)

    my ($nr1) = $user_input, "$a";

    This has no meaning. You are assigning  $user_input to  $nr1 (why?) and interpolating  $a (which has no value at this point) into a string, which you then throw away: what do you think is happening here? (Update: Two other, similar statements have the same problem.)

    Please consider these and other points raised so far, write some more code and post again.

    Update: Do continue to  use strict; and  use warnings; in your code (use Modern::Perl; includes both of these). They will make learning Perl much easier.

Re^3: Simple Arithmetic Question
by Anonymous Monk on Nov 06, 2014 at 04:02 UTC
Re^3: Simple Arithmetic Question
by Laurent_R (Canon) on Nov 06, 2014 at 07:26 UTC
    Either you get all 3 numbers separated by spaces on one line, chomp it once and then you have to use split or some other mechanism to get the individual numbers (the way I did it in my previous post), or you ask three times for one indivudal number from your user, get three times the input, chomp the input three times.
    print "give me a number\n"; my $nr1 = <>; chomp $nr1; print "give me a number\n"; my $nr2 = <>; chomp $nr2; print "give me a number\n"; my $nr3 ...