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

Discussion in 'Perl Misc' started by ed, Oct 3, 2005.

  1. ed

    ed Guest

    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?
    ed, Oct 3, 2005
    #1
    1. Advertising

  2. ed

    John Bokma Guest

    "ed" <> 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 :)
    John Bokma, Oct 3, 2005
    #2
    1. Advertising

  3. ed

    Dave Weaver Guest

    On 2 Oct 2005 19:34:24 -0700, ed <> 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;
    }
    Dave Weaver, Oct 3, 2005
    #3
  4. Dave Weaver wrote:
    > ed <> 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.lang.perl.misc/msg/415ffa6717dee81e

    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
    Gunnar Hjalmarsson, Oct 3, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. D. Shane Fowlkes

    just tinkering with a Sub Routine in ASP.NET

    D. Shane Fowlkes, Nov 12, 2003, in forum: ASP .Net
    Replies:
    5
    Views:
    427
    Scott M.
    Nov 13, 2003
  2. Neo
    Replies:
    6
    Views:
    399
    Mark A. Odell
    Dec 2, 2003
  3. Neo
    Replies:
    2
    Views:
    316
    Arthur J. O'Dwyer
    Dec 9, 2003
  4. Ben
    Replies:
    2
    Views:
    867
  5. Lawrence D'Oliveiro

    Death To Sub-Sub-Sub-Directories!

    Lawrence D'Oliveiro, May 5, 2011, in forum: Java
    Replies:
    92
    Views:
    1,980
    Lawrence D'Oliveiro
    May 20, 2011
Loading...

Share This Page