Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Rook question

by NetWallah (Abbot)
on Oct 18, 2012 at 02:46 UTC ( #999656=note: print w/replies, xml ) Need Help??


in reply to Rook question

Try this:
sub total{ my $sum = shift(@_); foreach (@_){ $sum += $_; } return $sum; }
  • add $_, not @_
  • return $sum
  • Add trailing } to match braces

             "By three methods we may learn wisdom: First, by reflection, which is noblest; Second, by imitation, which is easiest; and third by experience, which is the bitterest."           -Confucius

Replies are listed 'Best First'.
Re^2: Rook question
by squimby (Acolyte) on Oct 18, 2012 at 02:58 UTC
    Awesome! Fixed the problem--thanks much! I didn't know if Perl associated the default $_ while moving through the indices of @_.

      The mechanism of action is the foreach loop. It's just as happy aliasing $_ to elements of @this, @that, and @the_other as well. In this regard, @_ isn't special; foreach is.

      perlsyn


      Dave

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://999656]
help
Chatterbox?
beech testing in the sidebar
beech *me* me *me
beech *me* ... test
[beech]: ...
beech me *me* ... test me *me* test

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2017-03-28 23:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (343 votes). Check out past polls.