If I try to run the code you've shown* with strict and warnings, it does not compile. But both if I leave them off, and if I turn them on and change my $i, $imax; to my ($i, $imax); as it should be (the second actually declares two lexical variables), then the snippet you've provided works correctly for me, so this code does not actually reproduce the problem you are describing. This is why providing a representative Short, Self-Contained, Correct Example is important.

If I had to guess, maybe you're using a variable named $imax somewhere else in your code, and because of this, the $imax you're using in get_max_index is actually not local to that sub, and so some overlap is going wrong there. Always Use strict and warnings!

* Update: Code has been edited, original code that I was referring to is here.


In reply to Re: Why does my get_max_index function return zero? (High Water Mark Algorithm) by haukex
in thread Why does my get_max_index function return zero? (High Water Mark Algorithm) by hghosh

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.