Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Rook question

by squimby (Acolyte)
on Oct 18, 2012 at 02:33 UTC ( #999655=perlquestion: print w/ replies, xml ) Need Help??
squimby has asked for the wisdom of the Perl Monks concerning the following question:

So, I'm an utter noob learning Perl for work--and hopefully once I'm decent, to actually do things as well. Nonetheless, I'm trying to do an exercise in the O'Reilly camel book, but I can't get this subroutine working correctly:
sub total{ my $sum = shift(@_); foreach (@_){ $sum += @_; }
Any help? It always adds up to 17 when I include it in a script which throws in any number of arguments. Also: I'm trying to be more 'Perl Zen' but my code above looks like it'd be best friends with the cold and rigid likes of C++ or Java. Any tips for 'Perl'ifying this?

Comment on Rook question
Download Code
Re: Rook question
by NetWallah (Abbot) on Oct 18, 2012 at 02:46 UTC
    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

      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

Re: Rookie question
by Athanasius (Monsignor) on Oct 18, 2012 at 02:57 UTC
Re: Rook question
by sundialsvc4 (Monsignor) on Oct 18, 2012 at 13:21 UTC

    One of the trickiest notions in Perl is the issue of contexts.   When you wrote $sum += @_, you used an @array value in a $scalar context.   That changes what it does.

    For debugging such things, I would insert a statement within the loop that prints useful information to the STDERR file, e.g.:
      print STDERR "sum is $sum\n";.

    All interactive programs have two output streams, STDOUT and STDERR, the latter intended for error or diagnostic outputs.

    Also, get used to now putting these two statements at the start of every program:
      use strict;
      use warnings;

Re: Rook question
by blue_cowdawg (Prior) on Oct 18, 2012 at 13:35 UTC

    another solution:

    sub total { my $sum =0; map { $sum += $_ } @_; return $sum; }
    Couldn't resist... :-)


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg

      Let's fold a list.

      use List::Util 'reduce'; sub total { return reduce { $a + $b } 0, @_; }
        The above code hardly even resembles Perl as I know it. Got a lot to learn... I've heard at work they use 'map' regularly (I wouldn't say 'frequently'--something about it not processing efficiently with mysql in some contexts). I'll need to read up on that.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://999655]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2014-08-23 21:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (178 votes), past polls