Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: How can I add all the numbers in an array with out doing a foreach loop?

by lkench (Initiate)
on Jul 08, 2013 at 20:29 UTC ( [id://1043182]=note: print w/replies, xml ) Need Help??


in reply to How can I add all the numbers in an array with out doing a foreach loop?

my $sum = 0; $sum = [map {$sum +=$_} @arrayToSum]->[$#arrayToSum];
  • Comment on Re: How can I add all the numbers in an array with out doing a foreach loop?
  • Download Code

Replies are listed 'Best First'.
Re: Answer: How can I add all the numbers in an array with out doing a foreach loop?
by choroba (Cardinal) on Jul 09, 2013 at 09:27 UTC
    The problem is it does not work under strict and it is probably not doing what you think (it uses a global $sum and lexical $sum at the same time which is confusing).
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Answer: How can I add all the numbers in an array with out doing a foreach loop?
by Tux (Canon) on Jul 12, 2013 at 08:40 UTC

    List::Util is CORE:

    use List::Util qw( sum ); my $sum = sum @arrayToSum;

    Enjoy, Have FUN! H.Merijn

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1043182]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (2)
As of 2024-06-15 05:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.