Link to home
Start Free TrialLog in
Avatar of bmcquill
bmcquill

asked on

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?
ASKER CERTIFIED SOLUTION
Avatar of wilcoxon
wilcoxon
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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);
Avatar of bmcquill
bmcquill

ASKER

Both, no preset number size and no preset array size.
For numbers of arbitrary size, you can
use Math::BigFloat;
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
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.