Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Average/mean calculator

by Paradizingmania (Initiate)
on Jun 04, 2014 at 17:29 UTC ( #1088692=perlquestion: print w/replies, xml ) Need Help??
Paradizingmania has asked for the wisdom of the Perl Monks concerning the following question:

hey, i am a beginner to perl and programing in general, and i made a calculator that calculates the average of numbers, and i was just wondering if there was any shorter or easier way to do it.

print "In this version you can use up to TEN NUMBERS! \n"; print "when you have entered the numbers you need type 'end' \n"; print "Enter number 1 "; my $num1 = <STDIN>; print "Enter number 2 "; my $num2 = <STDIN>; print "Enter number 3 "; my $num3 = <STDIN>; if ($num3 =~ /^[+-]?\d+$/ ) { print "Enter number 4 "; my $num4 = <STDIN>; if ($num4 =~ /^[+-]?\d+$/ ) { print "Enter number 5 "; my $num5 = <STDIN>; if ($num5 =~ /^[+-]?\d+$/ ) { print "Enter number 6 "; my $num6 = <STDIN>; if ($num6 =~ /^[+-]?\d+$/ ) { print "Enter number 7 "; my $num7 = <STDIN>; if ($num7 =~ /^[+-]?\d+$/ ) { print "Enter number 8 "; my $num8 = <STDIN>; if ($num8 =~ /^[+-]?\d+$/ ) { print "Enter number 9 "; my $num9 = <STDIN>; if ($num9 =~ /^[+-]?\d+$/ ) { print "Enter number 10 "; my $num10 = <STDIN>; if ($num10 =~ /^[+-]?\d+$/ ) { print "Your answer is " . (($num1+ +$num2+$num3+$num4+$num4+$num5+$num6+$num7+$num8+$num9+$num10)/10) . " +\n"; } else { print "Your answer is " . (($num1+ +$num2+$num3+$num4+$num5+$num6+$num7+$num8+$num9)/9) . "\n"; } } else { print "Your answer is " . (($num1+$num +2+$num3+$num4+$num5+$num6+$num7+$num8)/8) . "\n"; } } else { print "Your answer is " . (($num1+$num2+$n +um3+$num4+$num5+$num6+$num7)/7) . "\n"; } } else { print "Your answer is " . (($num1+$num2+$num3+ +$num4+$num5+$num6)/6) . "\n"; } } else { print "Your answer is " . (($num1+$num2+$num3+$num +4+$$num5)/5) . "\n"; } } else { print "Your answer is " . (($num1+$num2+$num3+$num4)/4) . +"\n"; } } else { print "Your answer is " . (($num1+$num2+$num3)/3) . "\n"; } } else { print "Your answer is " . (($num1+$num2)/2) . "\n"; }

oh and i have not really been able to find any got tutorials so if you could link any that would be great. thanks in advance

Replies are listed 'Best First'.
Re: Average/mean calculator
by toolic (Bishop) on Jun 04, 2014 at 17:46 UTC


Re: Average/mean calculator
by Laurent_R (Abbot) on Jun 04, 2014 at 18:03 UTC
    Hi, store your numbers in an array or update the sum and count as you go, then you only need to divide by the count. Something like this:
    print "In this version you can use up to TEN NUMBERS! \n"; print "when you have entered the numbers you need type 'end' \n"; my $count = 1; my $total = 0; while ($count <= 10) { print "Enter number $count "; my $num = <STDIN>; chomp $num; last if $num eq "end"; $total += $num; $count++; } print " Average is: ", $total / $count, "\n";

      hey 3 questions, first what does the <= mean second what does the line $total += $num; and last what does the $count++ mean, i am still new to this so keep that in mind, and thanks for the help


        You should read the perlop documentation, it describes what all the operators are. Having said that:

        • $a <= $b means "$a is less than or equal to $b"
        • $total += $num means the same as $total = $total + $num
        • $count++ means the same as $count = $count+1


        When your only tool is a hammer, all problems look like your thumb.

        Hi Paradizingmania,

        Roboticus has given you the answers to your questions, but I should stress these questions relate to really basic Perl operators, and that you should absolutely take a serious look at the documentation pointed by him in his answer (perlop). Also take a look at the documentation pointed out by toolic, especially perlintro.

Re: Average/mean calculator
by wjw (Curate) on Jun 04, 2014 at 18:30 UTC

    Oft times I find that the best way to search for guidance, examples and solutions for Perl is simply to do a Google or DuckDuckGo search for precisely what I want. In this case, searching for 'perl average and mean calculator' gave me enough top level hits (including to nodes here at PM) to get started. Using SuperSearch works nicely as well, with just a bit more work...

    Hope that is helpful...

    ...the majority is always wrong, and always the last to know about it...

    Insanity: Doing the same thing over and over again and expecting different results...

    A solution is nothing more than a clearly stated problem...otherwise, the problem is not a problem, it is a facct

Re: Average/mean calculator
by Lennotoecom (Pilgrim) on Jun 05, 2014 at 07:11 UTC
    put in numbers continuously with enter, to do the calculations enter empty line
    ++$i and $s += $1 while($_ = <STDIN> and /(\d+)/); print "the average is: ",$s/$i,"\n";
Re: Average/mean calculator
by perlfan (Curate) on Jun 04, 2014 at 20:50 UTC
      Can you use things like PDL or Statistics::R?

      That's just cruel... Those modules are hardly suitable for someone who appears to be completely new to both Perl and programming in general.

      -- FloydATC

      Time flies when you don't know what you're doing

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1088692]
Approved by taint
Front-paged by toolic
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2017-08-18 21:18 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (310 votes). Check out past polls.