|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
|
|
#1
|
|||
|
|||
|
Stats program
Im doing an exercise in a book, and I dont understand one aspect.
theres a sub mean, which calculates the mean but the sub is called in this line, and i dont understand how it computes, could someone explain how it functions.. the line is Code:
return(mean($lower, $upper)); I understand what return does, and mean, but I dont see how the mean interacts with $lower and $upper. Thanks alot |
|
#2
|
||||
|
||||
|
Impossible to tell. mean is not a perl function. The subroutine is defined elsewhere. Check the book.
|
|
#3
|
|||
|
|||
|
mental blank on this line
Code:
return(mean($lower, $upper)); what does $upper and $lower do ?? Last edited by drjonesuk : April 24th, 2008 at 07:49 PM. |
|
#4
|
|||
|
|||
|
this is mean function, it then calls on a line of code I dont know how the code of line works, see below...
Code:
sub mean {
my (@data) = @_;
my $sum;
foreach(@data){
$sum+=$_;
}
return($sum/@data);
}
How does this line of code work??? All i see is 2 variable names next to a mean, and I got a blank, how does the 2 variables work Code:
return(mean($lower, $upper); I KNOW its simple, just another blank! Last edited by drjonesuk : April 24th, 2008 at 07:45 PM. |
|
#5
|
||||
|
||||
|
Code:
sub mean {
my (@data) = @_;
my $sum;
foreach(@data){
$sum+=$_;
}
return($sum/@data);
}
That's the subroutine. It can receive many values. Doesn't have to be two. The will all be collected in @data. The foreach adds all the values together, then returns the value divided by the number of entries. If you wanted to use that subroutine, you would call it like this: Code:
#!/usr/bin/perl
use strict;
use warnings;
my $value = mean(1,2,3,4);
print "$value\n";
sub mean {
my (@data) = @_;
my $sum;
foreach(@data){
$sum+=$_;
}
return($sum/@data);
}
But you don't have to waste the variable if you don't want to. You could just print the value directly: Code:
print mean(1,2,3,4); And in the same way, you could return that directly from another subroutine. Code:
return mean(1,2,3,4); The two variables are just two numbers they want to average: Code:
#!/usr/bin/perl
use strict;
use warnings;
my $one = 5;
my $two = 10;
print mean($one, $two);
sub mean {
my (@data) = @_;
my $sum;
foreach(@data){
$sum+=$_;
}
return($sum/@data);
}
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Perl Programming > Stats program |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|