Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Confused newbie, trying to get a sub routine to work.

Reply
Thread Tools

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

 
 
ed
Guest
Posts: n/a
 
      10-03-2005
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?

 
Reply With Quote
 
 
 
 
John Bokma
Guest
Posts: n/a
 
      10-03-2005
"ed" <(E-Mail Removed)> wrote:

> 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?

--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
I ploink googlegroups.com

 
Reply With Quote
 
 
 
 
Dave Weaver
Guest
Posts: n/a
 
      10-03-2005
On 2 Oct 2005 19:34:24 -0700, ed <(E-Mail Removed)> wrote:
> 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;
}

 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      10-03-2005
Dave Weaver wrote:
> ed <(E-Mail Removed)> wrote:
>> 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, ...

>
> 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....5ffa6717dee81e

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

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Death To Sub-Sub-Sub-Directories! Lawrence D'Oliveiro Java 92 05-20-2011 06:50 AM
Recognising Sub-Items and sub-sub items using xslt Ben XML 2 09-19-2007 09:35 AM
SUMMARY: How to return an array from a sub-routine. Neo C Programming 2 12-09-2003 04:59 PM
How to return an array from a sub-routine. Neo C Programming 6 12-02-2003 03:53 PM
just tinkering with a Sub Routine in ASP.NET D. Shane Fowlkes ASP .Net 5 11-13-2003 02:15 AM



Advertisments