cooldood3490
cooldood3490

Reputation: 2498

Subroutine that takes average of one or more arrays

I'm working on a subroutine that takes the average of 1 or more arrays. I would like to do this without using a module.

use strict;
use warnings;

use List::Util 'sum';

my @w = (0, 2);
my @x = (1, 3);
my @y = (2, 2);
my @z = (1, 1);

# the average of these four arrays is (1,2) since
# (0+1+2+1)/4 = 1 and (2+3+2+1)/4 = 2    

my @arrays = \(@w, @x, @y, @z);

my @avg;
# this is the way to do it using the module
for my $i (0..$#w) {
    $avg[$i] =  sum(map $_->[$i], @arrays) / @arrays;
}
print "@avg\n";

# my way of doing it without module
@avg;
for my $i (0..$#w) {
    $avg[$i] = prod_sum(map $_->[$i], \@arrays) / @arrays;
}

print "@avg\n";


# subroutines
sub prod_sum{
    my $o = $_[0];
    my $arr_ref = $_[1];
    my $array_ref;

    foreach my $row (@$arr_ref){
        foreach my $cell (@$row) {
            push(@{ $array_ref }, $_);
        }
    }

    my $sum = $o + the_sum($array_ref);
    return $sum;
}

sub the_sum{
    my $sum = 0;
    for ( @{$_[0]} ) {
        $sum += $_;
    }
    return $sum;
}

output

1 2
[pair of really random big numbers]

The first output is correct. It displays the average of all of the arrays. The second output is completely wrong. How do I do this without using a module?

Upvotes: 0

Views: 156

Answers (2)

imran
imran

Reputation: 1560

You think sum is being passed two variables, it is not. It is only being passed an array. Modify your prod_sum to expect only an array (and replace \@arrays in the call of prod_sum to be just @arrays). Or you can use this:

sub sum {
  return 0 if $#_ < 0;
  my $head = shift;
  return $head + sum(@_);
}

The above is a recursive subroutine that will sum an array.

Note: if your array has more then 100 element, use warnings will emit a deep recursion warning. For more on that topic, see here

Upvotes: 1

Miguel Prz
Miguel Prz

Reputation: 13792

I propose this solution:

use strict;
use warnings;

my @w = (0, 2);
my @x = (1, 3);
my @y = (2, 2);
my @z = (1, 1);

my @arrays = \(@w, @x, @y, @z);

my ($x, $y) = (0, 0);
foreach my $arr(@arrays) {
    $x += $arr->[0];
    $y += $arr->[1];
}

my @result = ( $x / @arrays,  $y / @arrays);

print "(@result)", "\n";  # <---- prints (1 2)

Upvotes: 1

Related Questions