Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: recursively reduce an array

by rjt (Deacon)
on Nov 05, 2012 at 22:56 UTC ( #1002398=note: print w/replies, xml ) Need Help??


in reply to recursively reduce an array

You say you want to recursively sort an array, so I'll assume you've already done something like:

my @sorted = sort keys %hash;

From there, you mentioned you want to recursively take 10 elements at a time. It's very easy to do this iteratively, but to specifically do it recursively as you've asked is easy, too:

my @sorted = ('A'..'Z'); # probably sort keys %some_hash; reduce(@sorted); sub reduce { my @ten = splice(@_, 0, 10); return if (!@ten); # Base case # return if (@ten < 10); # Or do you want to stop if < 10 elements +? print join(',',@ten) . "\n"; return reduce(@_); # Recursion case, remainder of array }

Have a look at this page on recursion if you need to brush up on your recursion theory. For example, you can switch the recursion case and the processing step, and of course you may want to return a value derived from your computations on @sorted. I leave these as an exercise to the OP. :-)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1002398]
help
Chatterbox?
[Corion]: thezip: Yeah - we have "CAP", but that means "Change Ablauf Plan" (Change Sequence Plan). Likely there is a true ITIL name for this too.
[Corion]: While I'm not really a fan of ITIL, it certainly is better than nothing and it at least provides a common vocabulary, so if you have ITIL somewhere at your disposal, it might be worth a look
[Corion]: Hmm - that should be somewhere in the "Release Deployment Plan", but I don't find a good English link (nor a good German one ;) )
[thezip]: My project manager is asking "Why do we need this?" Ahem...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2017-02-23 17:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?






    Results (350 votes). Check out past polls.