merc58
Technical User
- Aug 18, 2008
- 4
Can someone please tell me what I'm doing wrong. I'm trying to use three subroutines in one lib file to calculate total, average and extend the array by adding new values from previous values divided by 2
--------------------------------------------------------------
#! /usr/bin/perl
require 'obj13-lib.pl';
@field = <STDIN>;
foreach $fields (@field) {
$total_num = sumup($fields);
$avg_num = averager($fields);
$ext_array = extender(@fields);
print "The Total Number of Array Elements is $total_num\n";
print "The Average Number of Array Elements is $avg_num\n";
print "The New Extended Array is $ext_array\n";
}
------------------------------------------------------------
sub sumup() {
my $i = 0;
my $fields = shift(@_);
foreach $fields (@field) {
$total = $fields[$i] + $total;
}
return $total;
}
sub averager(){
my $i = 0;
my $fields = shift(@_);
foreach $fields (@field) {
$total = $fields[$i] + total
}
$avg = $total/($#field + 1);
return $avg;
}
sub extender() {
my $i = 0;
my $fields = shift(@_);
foreach $fields (@field){
@newfield = $fields[$i]/2;
foreach $line (@newfield){
push(@field, $newfields[$i])
}
}
return @field;
}
1;
--------------------------------------------------------------
#! /usr/bin/perl
require 'obj13-lib.pl';
@field = <STDIN>;
foreach $fields (@field) {
$total_num = sumup($fields);
$avg_num = averager($fields);
$ext_array = extender(@fields);
print "The Total Number of Array Elements is $total_num\n";
print "The Average Number of Array Elements is $avg_num\n";
print "The New Extended Array is $ext_array\n";
}
------------------------------------------------------------
sub sumup() {
my $i = 0;
my $fields = shift(@_);
foreach $fields (@field) {
$total = $fields[$i] + $total;
}
return $total;
}
sub averager(){
my $i = 0;
my $fields = shift(@_);
foreach $fields (@field) {
$total = $fields[$i] + total
}
$avg = $total/($#field + 1);
return $avg;
}
sub extender() {
my $i = 0;
my $fields = shift(@_);
foreach $fields (@field){
@newfield = $fields[$i]/2;
foreach $line (@newfield){
push(@field, $newfields[$i])
}
}
return @field;
}
1;