Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9187641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:49:23+00:00 2026-06-17T19:49:23+00:00

My script needs to get a series of numbers input by the user and

  • 0

My script needs to get a series of numbers input by the user and find the average of them. I would like to use the line ‘end-of-file’ to show that the user is done inputting code. Any help would be appreciated. Below is what I have so far. I think I am really close, but I am missing something.

Code:

#! /usr/bin/perl
use 5.010;


print "Enter the scores and type end-of-file when done";
chomp(@scores = <STDIN>);
foreach (@scores) {
    push_average(total(@scores));
}




sub total {
    my $sum;
    foreach (@_) {
        $sum += $_;
    }
    sum;
}

sub average {
    if (@_ == 0) {return}
    my $count = @_;
    my $sum = total(@_);
    $sum/$count;
}

sub push_average {
    my $average = average(@_);
    my @list;
    push @list, $average;
    return @list;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T19:49:24+00:00Added an answer on June 17, 2026 at 7:49 pm

    You are quite close. Adding use strict; use warnings at the top of every Perl script will alert you of errors that might go unnoticed otherwise.

    A few hints:

    • You forgot the sigil of $sum in the last statement of total. Currently, you return a string “sum” (without strict vars), or possibly call a sub called sum.

    • You don’t need the foreach in the main part, rather do

      my @averages = push_average(@scores);
      

      The total is already calculated inside push_average

    • You probably want to print out the resulting average:

      my $avg = $averages[0];
      say "The average of these numbers is $avg";
      
    • The push_average is silly; you return a new array of one element. You could return that one element just as well.


    Suggested script:

    use strict; use warnings; use 5.010;
    use List::Util qw/sum/;  # very useful module
    
    # say is like print, but appends a newline. Available with 5.10+
    say "Please enter your numbers, finish with Ctrl+D";
    my @nums = <STDIN>;
    chomp @nums;
    
    # The // is the defined-or operator
    # interpolating undef into a string causes a warning.
    # Instead, we give an expressive message:
    my $avg = average(@nums) // "undefined"; 
    
    say "The average was $avg";
    
    sub average { @_ ? sum(@_) / @_  : undef } # return undef value if called without args
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to get the remote user name in my CGI script. Where do
My perl script needs to ask the user for a password to download some
Im working on a script that needs to find a match for certain letters(chords)
I'm currently developing what is more or less a script that needs to get
I am trying to customize a script and need to get a POST value
Perl script needs to receive ajax request, send a success message back to browser,
I have a Python script that needs to issue a number of shell commands.
I'm working on a Python script that needs to create about 50 distinct temporary
So I have a bash script that needs to take an arbitrary number of
for example I have a script that needs to put it's parent directory on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.