in reply to how can i hold temporary values in a recursive function

I can only guess what you want, maybe its a closure to memorize the recursive results?

{ my %memo; sub rec_func { my $key=key_from_input(@_); unless (exists $memo{$key}) { $memo{$key}=calculate(@_); } return $memo{key}; } }

This should effectively cache complicated calculations based on the call-parameters...

(untested)

Of course you are free to decide what you are caching...

Cheers Rolf

Replies are listed 'Best First'.
Re^2: how can i hold temporary values in a recursive function
by Corion (Patriarch) on Apr 18, 2010 at 16:51 UTC
      I suppose he needs a more general flexibility to memoize based on more complicated input ("Level"?) Thats why I didn't mention Memoize

      For those interested see Viterbi Algorithm and Algorithm-Viterbi

      Cheers Rolf

        You can pass a key-generator function to Memoize in case you need to normalize your parameters. Personally, I would add the "level" as another parameter and use plain Memoize.