Hi, I tried to run your first piece of code to better visualize the data, but even that part does not work properly and probably never ends. These nested loops don't work:
for(my $i=1; $i<=$x; ++$i){ for(my $j=0; $j<$y; ++$j){ for(my $k=0; $k<$y; ++$j){ $distance_mat -> [$i][$j][$k] = rand 1; } } }
because the third line increments $j and tests max value on $k, so that will probably never ends. A minimum correction on the first part would be:
use strict; use warnings; use Data::Dumper; my $x = 4; my $y = 5; my $distance_mat; #################### for(my $i=1; $i<=$x; ++$i){ for(my $j=0; $j<$y; ++$j){ for(my $k=0; $k<$y; ++$k){ $distance_mat -> [$i][$j][$k] = rand 1; } } } ##############Start and end nodes have zero cost for(my $i=0; $i<scalar(@{$distance_mat->[1]}); ++$i){ $distance_mat -> [0][0][$i] = 0; } for(my $i=0; $i<scalar(@{$distance_mat->[-1]}); ++$i){ $distance_mat -> [-1][0][$i] = 0; } print Dumper $distance_mat;
which will print out:
$VAR1 = [ [ [ 0, 0, 0, 0, 0 ] ], [ [ '0.212283829766424', '0.693160487196536', '0.721569572380883', '0.0147371758675447', '0.508071977540641' ], [ '0.607456981189884', '0.413801502805963', '0.962572895729942', '0.938935399978082', '0.717291943322152' ], [ '0.306279826459825', '0.89992134970587', '0.690479726793445', '0.733121785882545', '0.487829205187605' ], [ '0.711943004833408', '0.254652102915149', '0.267465127747556', '0.302759025077147', '0.32486696921206' ], [ '0.489091147270205', '0.0735180399392874', '0.235300433159548', '0.746479779929494', '0.905501634741288' ] ], [ [ '0.688008245370213', '0.113680627990203', '0.99717709784958', '0.510060769658413', '0.767946103966395' ], [ '0.947138589977666', '0.369698435985974', '0.651511387205236', '0.211410103277188', '0.21735370417333' ], [ '0.734569009367142', '0.598363562918809', '0.431561123751734', '0.712531952111899', '0.293976071886583' ], [ '0.517267742541694', '0.55231451959105', '0.45234384270081', '0.147473762126175', '0.290022437346682' ], [ '0.470738612969299', '0.0427293275962448', '0.777329578040955', '0.144845576196932', '0.608144103958836' ] ], [ [ '0.012097916474044', '0.488911202200264', '0.424603577528178', '0.187457713190881', '0.608602384605799' ], [ '0.492303567493103', '0.334913914644719', '0.232936661637211', '0.929022161683616', '0.616006850324688' ], [ '0.150798208707261', '0.409304914407997', '0.553553019401473', '0.173385566856545', '0.882371508058217' ], [ '0.786329035626949', '0.480783348396823', '0.719431190274186', '0.656542212578945', '0.632677581767197' ], [ '0.699789445283013', '0.941904765904233', '0.239623890103449', '0.176224763809842', '0.261173130161335' ] ], [ [ 0, 0, 0, 0, 0 ], [ '0.373621309619981', '0.411533490541188', '0.623695973015582', '0.00773593933377725', '0.00893442880364503' ], [ '0.837186783303693', '0.584918955098058', '0.0295591991893147', '0.422034731454435', '0.260471634650969' ], [ '0.82811137679651', '0.49858938564811', '0.153148378721244', '0.500505552716803', '0.679154015831099' ], [ '0.0258503603085316', '0.399454916212452', '0.470265378306834', '0.498481414135323', '0.832463141199078' ] ] ];
Notice how Data::Dumper makes it possible to visualize your data structure. Looking at the above structure, it would seem (but I cannot be sure) that you would probably want to use the post increment operators, rather than the pre increment operator. Or, better, use the
for my $i (0..$max_val)
syntax. Please fix this part of your code so that we can understand what you are trying to do and agree on your input, then only can we think really on the rest of your program. And, at this point, you probably want to open a new thread (referring to this one) with the input part properly fixed, son that we can then focus on the rest.

In reply to Re: Viterbi application by Laurent_R
in thread Viterbi application by azheid

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.