Perl sum and product example

In this Perl example I calculate the sum and product from a array:

#!/usr/bin/perl
use strict;

#example data
my @array = (2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14);

# test subs
print sum(@array)."n";
print product(@array)."n";

#calc sum
sub sum {
    my @array = @_;
    my $sum = 0;
    for(@array){$sum += $_;}
    return($sum);
}

#calc product
sub product {
    my @array = @_;
    my $product = 1;
    for(@array){$product *= $_;}
    return($product);
}

And we got our result:

119
25169356800

One thought on “Perl sum and product example”

  1. This is really fascinating, You’re an overly
    skilled blogger. I have joined your feed and sit up for seeking more
    of your wonderful post. Additionally, I’ve shared your site in my social networks

Leave a Reply

Your email address will not be published.

2 × one =