Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old April 24th, 2008, 07:07 PM
drjonesuk drjonesuk is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 145 drjonesuk User rank is Corporal (100 - 500 Reputation Level)drjonesuk User rank is Corporal (100 - 500 Reputation Level)drjonesuk User rank is Corporal (100 - 500 Reputation Level)drjonesuk User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 5 h 37 m 23 sec
Reputation Power: 9
Send a message via ICQ to drjonesuk
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

Reply With Quote
  #2  
Old April 24th, 2008, 07:24 PM
keath's Avatar
keath keath is offline
!~ /m$/
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: May 2004
Location: Leawood, Kansas
Posts: 2,513 keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 9 h 9 m
Reputation Power: 527
Impossible to tell. mean is not a perl function. The subroutine is defined elsewhere. Check the book.

Reply With Quote
  #3  
Old April 24th, 2008, 07:26 PM
drjonesuk drjonesuk is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 145 drjonesuk User rank is Corporal (100 - 500 Reputation Level)drjonesuk User rank is Corporal (100 - 500 Reputation Level)drjonesuk User rank is Corporal (100 - 500 Reputation Level)drjonesuk User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 5 h 37 m 23 sec
Reputation Power: 9
Send a message via ICQ to drjonesuk
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.

Reply With Quote
  #4  
Old April 24th, 2008, 07:30 PM
drjonesuk drjonesuk is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 145 drjonesuk User rank is Corporal (100 - 500 Reputation Level)drjonesuk User rank is Corporal (100 - 500 Reputation Level)drjonesuk User rank is Corporal (100 - 500 Reputation Level)drjonesuk User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 5 h 37 m 23 sec
Reputation Power: 9
Send a message via ICQ to drjonesuk
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.

Reply With Quote
  #5  
Old April 24th, 2008, 08:44 PM
keath's Avatar
keath keath is offline
!~ /m$/
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: May 2004
Location: Leawood, Kansas
Posts: 2,513 keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 9 h 9 m
Reputation Power: 527
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);
}

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > Stats program


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway