Confused newbie, trying to get a sub routine to work.

E

ed

I have a script that works and i'm trying to set up as a subroutine
also, but I can't get it to work as a sub routine, here is my script
so far:
#!/usr/bin/perl

@params = (1,3,2,4,5,7,8,9);

foreach $avg(@params){
$total = numbers($avg);
print "$total\n";
}
sub numbers(){
my $avg = shift(@_);

my $sum = 0, $avg = 0;

my@half;
foreach(@params){
$sum += $_;
push @half, $_/2;
$avg = ($sum / @params);

$return $total;
}
}

I'm trying to write a script that will calculate the average of the
numbers, the total of all the numbers added together, and a new array
of number which is the other numbers divided by 2. What am I missing?
 
J

John Bokma

ed said:
I have a script that works and i'm trying to set up as a subroutine
also, but I can't get it to work as a sub routine, here is my script
so far:
#!/usr/bin/perl

@params = (1,3,2,4,5,7,8,9);

no use strict; no use warnings;

Please read the posting guide lines.
foreach $avg(@params){
$total = numbers($avg);
print "$total\n";
}
sub numbers(){

Why do you have () there?
 
D

Dave Weaver

I have a script that works and i'm trying to set up as a subroutine
also, but I can't get it to work as a sub routine, here is my script
so far:
#!/usr/bin/perl

Ask perl for all the help it can give you:

use warnings;
use strict;

After adding the above line you'll need to make sure you declare
all your variables (using "my").

By the way, all this is mentioned in the posting guidelines that are
posted here regularly. If you read and conform to these guidelines
then it will greatly increase your chances of a useful answer here. If
you fail to conform (such as by posting programs that *don't* use
strict/warnings) then it will reduce the chances that the very people
most able to help will actually look at your post.
@params = (1,3,2,4,5,7,8,9);

foreach $avg(@params){
$total = numbers($avg);

So what is "numbers" supposed to return? (the name "numbers" doesn't
give much of a clue; name subroutines to give a clear idea of what
they do). Note that you are calling numbers() once for each value in
@params.
print "$total\n";
}
sub numbers(){
^^
These parentheses don't do whatever it is you think they do. Take
them out:
sub numbers {
my $avg = shift(@_);
my $sum = 0, $avg = 0;

my@half;
foreach(@params){

For each value in @params, you're calling this sub; this sub then
loops over @params a second time - I think you need to sit down and
work out you algorithm properly. Try pen & paper to make a flow chart
of what you want to do.
$sum += $_;
push @half, $_/2;
$avg = ($sum / @params);

$return $total;

"$return"?? isn't that supposed to be "return"?
And you're returning "$total", a variable that this sub hasn't even
used. What's it supposed to be?

Do you actually understand how this program is *supposed* to work?
}
}

I'm trying to write a script that will calculate the average of the
numbers, the total of all the numbers added together, and a new array
of number which is the other numbers divided by 2. What am I missing?

It sounds like you're missing some basic computer science skills...

I suggest you:

* think about your algorithm

* work out a flow chart that describes it

* translate the steps in the flowchart into some sort of pseudo-code

* now write the Perl code using your pseudo-code as a guide,
remembering to "use warnings" and "use strict"

* give variables and subroutines descriptive (but not overly
long) names, that accurately describe what they hold/do.

* remember, as a rule of thumb, subroutines should only access
variables that have been passed to it via its argument list; don't
let them access "global" variables - it'll only make your code
into spaghetti that you won't understand when you come back to it
later.

To give you an idea, here's a simple program to calculate the sum of a
list of numbers. See if you can adapt it to calculate the average and
halves (hint: use 3 subroutines; one to calculate a sum, one to
calculate the average, and one to work out the halves)

#!/usr/bin/perl

use warnings;
use strict;

my @numbers_to_sum = ( 1 .. 10 );

my $total = calculate_sum( @numbers_to_sum );
print "The sum of (@numbers_to_sum) is $total\n";
exit;

sub calculate_sum {
my ( @numbers ) = @_;

my $sum = 0;

for my $number ( @numbers ) {
$sum += $number;
}

return $sum;
}
 
G

Gunnar Hjalmarsson

Dave said:
Ask perl for all the help it can give you:

use warnings;
use strict;

After adding the above line you'll need to make sure you declare
all your variables (using "my").

By the way, all this is mentioned in the posting guidelines that are
posted here regularly. If you read and conform to these guidelines
then it will greatly increase your chances of a useful answer here. If
you fail to conform (such as by posting programs that *don't* use
strict/warnings) then it will reduce the chances that the very people
most able to help will actually look at your post.

Well said, but probably wasted on that "ed" character.
http://groups.google.com/group/comp.lang.perl.misc/msg/415ffa6717dee81e

He seems to just keep trying till somebody writes his (homework?)
program for him.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top