Beefy Boxes and Bandwidth Generously Provided by pair Networks chromatic writing perl on a camel
Perl: the Markov chain saw
 
PerlMonks  

Re: how to avoid nested looping?

by davido (Bishop)
on Feb 04, 2005 at 06:39 UTC ( #427952=note: print w/ replies, xml ) Need Help??


in reply to how to avoid nested looping?

In order to sum up all the leaves of a nested datastructure, you have to look at each and every leaf. This means nested iteration, or in other words, nested looping. Yes, you can hide the looping in recursion, but it's still looping, in a different way. There's no way of getting around the fact that you have to inspect each leaf.

Perhaps you could avoid this step by keeping a running total as you accumulate the leaves into the nested structure. I don't know where you're getting the data that fills this datastructure, but if you find it expensive (from a computational standpoint) to wend your way through the datastructure adding its nodes, try looking at the problem in a way that avoids having to do that. If you can keep a running tally, you save yourself from the looping, at the expense of a little computational work each time you add, remove, or modify an element in the structure.


Dave


Comment on Re: how to avoid nested looping?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (8)
As of 2013-05-25 07:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best material for plates (tableware) is:









    Results (519 votes), past polls