bluethundr has asked for the wisdom of the Perl Monks concerning the following question:
It's been a while. Both since I've used Perl and come here seeking knowledge and your kind help.
Since it's been a while, I decided I would "ride the Llama" once more. So far, I've just been cracking along and enjoying a nice refresher.
But unfortunately, I seem to have hit a snag in the first exercise on chapter 4. I've stared and stared at my code...and stared some more. When I was done staring, I stared one more time.
Now. I've decided to cave and ask for help. I just don't know why my 'total' function is behaving so oddly. Here's the proglet:
----------------------------------------------------------
------------------------------------------------------#!/usr/bin/perl use strict; sub total { my $number; foreach (@_) { $number += @_[$_]; } return $number; } 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: \n"; my $user_total = &total(<STDIN>); print "The total of those numbers is $user_total.\n\n";
Danke!
2006-10-01 Retitled by g0n, as per Monastery guidelines
Original title: '0dd llama'
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Having problems with addition in sub
by atcroft (Abbot) on Oct 01, 2006 at 04:18 UTC | |
by GrandFather (Saint) on Oct 01, 2006 at 04:45 UTC | |
|
Re: Having problems with addition in sub
by grep (Monsignor) on Oct 01, 2006 at 03:48 UTC | |
by ysth (Canon) on Oct 01, 2006 at 03:51 UTC | |
|
Re: Having problems with addition in sub
by ikegami (Patriarch) on Oct 01, 2006 at 16:51 UTC | |
|
Re: Having problems with addition in sub
by ghoti (Acolyte) on Oct 01, 2006 at 17:17 UTC | |
|
Re: Having problems with addition in sub
by jbert (Priest) on Oct 02, 2006 at 12:58 UTC | |
|
Re: Having problems with addition in sub
by halley (Prior) on Oct 02, 2006 at 14:51 UTC | |
|
Re: Having problems with addition in sub
by Anonymous Monk on Oct 02, 2006 at 08:19 UTC |