hello, please read question. dont just say "use my". it will be a long question and i am not sure if i can tell correctly. i will try. i want to implement viterbi algorithm in a recursive way. i can do it in a iterative way by assigning each values at each level in forward way. but i want to do it with recursion in backward way. (i have stuck in a similar problem so i want to learn this) i am stuck in a place where "i want to assign a recursive function to all keys of a hash" and return it. ( of course all those keys recall that function again) .but i cant hold those temporary values. i can hold but those get mixed with calculation of other levels. for example;
foreach my $yy ( keys %{$trans->{$state}} ) { $trans->{$state}->{$yy} * viterbi($yy, $r) ; }
this part is main part. all code is here :
@observation = qw(empty a a empty); $trans = { 'end' => {M2 => 1}, 'M2' => { I1 => 0.1, M1 => 0.3}, 'I1' => { begin => 0.8}, 'M1' => { begin => 0.2}, }; sub viterbi { my $state = shift; my $output = shift; # V (begin)(0) =1; V(anyother)(0) =0; V(begin)(other_numbers) =0; if($state eq begin) { if($output == 0) { return 1;} else { return 0; } + } elsif ( ($state ne begin) && ($output == 0) ) {return 0; } else { $r=$output-1; #maximum of below foreach# foreach my $yy ( keys %{$trans->{$state}} ) { $trans->{$state}->{$yy} * viterbi($yy, $r) ; } } } print "\n\n", viterbi(end, 3);
i didnt add any of my wrong codes to main foreach part, for those who want to try the code after knowing what to do, answer must be 0.08. for those who know viterbi, i didnt add emission probabilities because it is the easy part i guess. but i really want to know how to make the program hold all temporary values for all recursive calculations. if i put them in an array, program adds values not layer by layer but level by level, so things doesnt work. iterative solution is possible but i cant figure out how to solve it in a recursive way. thanks so much whoever helps. thanks again

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.