Perl Tutorial

I am doing a Perl tutorial and I have a problem posed: Create a library with a function that takes in an array of numbers (of arbitrary size). The function will then calculate the average of the numbers, the total of all of the numbers added together, and a new array of numbers which is comprised of the original input numbers each divided by 2. It will then return a new list with all of that information. What I'm stuck on is how to write a script that creates the array and pass that to the library, completing the steps above. Anyone have similar examples?
bmcquillAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

wilcoxonCommented:
#!/usr/local/bin/perl
use strict;
use warnings;
use MyModule;
die "Usage: $0 list_of_numbers\n" unless @ARGV;
print join "\n", do_stuff(@ARGV), "\n";

Open in new window


use strict;
use warnings;
package MyModule;
require Exporter;
@EXPORT = qw(do_stuff);
sub do_stuff {
    my (@arr) = @_;
    my $tot;
    foreach my $n (@arr) { $tot += $n; }
    return ($tot/@arr, $tot, map { $_ / 2 } @arr);
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ozoCommented:
Are the numbers of arbitrary size, or is the array of arbitrary size?

use mylibrary;
my @array=(1,2,3,5,8);
my @newlist=function(@array);
bmcquillAuthor Commented:
Both, no preset number size and no preset array size.
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

ozoCommented:
For numbers of arbitrary size, you can
use Math::BigFloat;
arnoldCommented:
Not sure I understand what your difficulty is, you would pass a reference to the array.

You create @array
Call you function with \@array
Your function will have $array_reference to expect
Then @$array_reference will grant your library/function access to the array elements/data
wilcoxonCommented:
Math::BigInt would be better than Math::BigFloat if the numbers must be integers (and not floating point).  However, perl natively handles fairly large numbers so you may not need any Math::Big* module.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.