Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

passing values to subroutine

Status
Not open for further replies.

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;

 
start with
use strict

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
How do I input array values from <STDIN> is my approach right?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top