[EMAIL PROTECTED] (Dragon Nebula Web Design) writes:

[...]

> In this case, RTFMing is not helping (and I'd rather not
> resort to the answer in the back of the book).

You chose to not look?
  Don't let your pride get in the way of working code.  AND
  also, don't let a simple answer get in the way of a good
  learning exercise.  That 'ol yin/yang thing.

Or did you look and not find it helpful?


[...]

> My question is, what am I not doing right? I don't want the answer to
> the program, just a point in the right direction in TFM. BTW, this
> exercise is only supposed to take 12 min or so, I've been working on it
> for nearly 4 hrs total.

Breathe...  In.  Out.  Repeat.  Again.

Take stock of what you *did* learn.  Don't forget it.  You
invested 4 hours in that lesson!!!!

> #!usr/local/bin/perl -w
> 
> my @fred = qw{ 1 3 5 7 9 };
> my $fred_total = &total (@fred);
> print "The total of \@fred is $fred_total.\n";
> print "Enter some numbers on separate lines: ";
> my $user_total = &total (<STDIN>);
> print "The total of those numbers is $user_total.\n";
> 
> sub total {
>   ($fred_total...$fred_total +=) ;
>   (<stdin>...<stdin> +=) ;
>  }

You're missing the parameter passing mechanism.  Look again
(in TFM) at how parameters are passed into a sub.  But don't
look about 20 lines below if you don't want to see a
solution.


Enjoy the learning exercise,
Michael

Answers below
      |
      |
      |
      V
      |
      |
      |
      V
      |
      |
      |
      V
      |
      |
      |
      V
      |
      |
      |
      V


# Readable.
sub total {
    my $sum;
    foreach my $num (@_) {
        $sum += $num;
    }
    return $sum;
}


# Streamlined
sub total {
    my $sum;
    $sum += $_ foreach (@_);    # $_ implicitly set
}   # sum implicitly returned


-- 
Michael R. Wolf
    All mammals learn by playing!
        [EMAIL PROTECTED]

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to