Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

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

by Adam (Vicar)
on Jun 09, 2000 at 23:48 UTC ( [id://17417]=note: print w/replies, xml ) Need Help??


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

Right, I was just pointing out that using 'for' doesn't answer the question of not using a 'foreach' loop. It looks kind of funny when the question stipulates that the person doesn't want to use something, and then you use it in your answer. Thats all.
  • Comment on RE: RE: RE: Answer: How can I add all the numbers in an array with out doing a foreach loop?

Replies are listed 'Best First'.
RE: RE: RE: RE: Answer: How can I add all the numbers in an array with out doing a foreach loop?
by KM (Priest) on Jun 09, 2000 at 23:52 UTC
    I gave two ways to do it. There is no way to do it without some sort of loop happening somewhere. It is also why I said that that is the loopish way. Sorry if I gave too much information for your taste. I see your answer.. oh wait.. you didn't have an answer.

    Cheers,
    KM

      Its unfortunate, KM, that you didn't really read the question, or my posts for that matter. You might have noticed that I was not putting you down or saying that your answer sucked. I was merely pointing out that you gave an answer that ignored the question, followed by one that didn't. As for why I hadn't posted my own answer? Thats because I liked your second answer. But you want one from me that doesn't use a loop? Ok. I'll use recursion then:
      sub addArray{ shift(@_) + addArray(@_) if(@_) } $total = addArray( 1, 3, 8, 15, 7, 5, 3 )

      Update: Two other things KM, I notice that you voted -- on my original comment. I'm not sure why. Nothing I said in that post was incorrect or rude. I also have noticed that my recursive answer is similar to the one that Btrott posted. I'm not sure why I should care, but I didn't want you to think I had copied it, as I had not read it when I first posted this. Cheers.

        Recursion is a looping construct as well. As everybody keeps pointing out there isn't really a way to do this without some kind of loop going on somewhere.

        Nuance

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-06-22 06:32 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.