Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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

by SimonSaysCake (Beadle)
on Mar 09, 2017 at 16:51 UTC ( [id://1184051]=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?

He's a rather ridiculous solution which avoids using the addition operator. It only works for positive integers.

my @quans = (2, 5, 8, 12, 15, 88); my $total = scalar(map {1 .. $_} @quans); print "$total.\n";
  • 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 Mar 09, 2017 at 17:00 UTC
    This is very ineffective way of counting the sum. Try adding 100000000 into the array.

    What your solution does is it creates ranges 1 .. $_ for each number in the list, and then counts how many members the whole sequence has.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-06-18 12:19 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.