Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Why does my get_max_index function return zero? (High Water Mark Algorithm)

by haukex (Archbishop)
on Jun 03, 2019 at 17:04 UTC ( [id://11100890]=note: print w/replies, xml ) Need Help??


in reply to Why does my get_max_index function return zero? (High Water Mark Algorithm)

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.

Replies are listed 'Best First'.
Re^2: Why does my get_max_index function return zero? (High Water Mark Algorithm)
by hghosh (Acolyte) on Jun 03, 2019 at 17:45 UTC
    Sorry, I copy-pasted the original script's code (which didn't use warnings or strict). Please see my edited code above because my script is still returning zero, even when I run the function as its own perl script (I just run the above code in a file called test_max.pl). Also, I don't use $imax anywhere else in the original script that uses the function "get_max_index".
      The $imax inside your for loop is different from the $imax outside it (as you've declared it with my inside the for loop). Remove that my.
      Please see my edited code above

      Please have a look at How do I change/delete my post?, in particular, "It is uncool to update a node in a way that renders replies confusing or meaningless". But luckily I still had your original code sitting around:

      Other than that, Paladin has already pointed out the issue with the second my declaration inside the loop in your updated code. I just wanted to add that in general, one should not combine my with an if statement modifier, as its behavior is undefined (see Statement Modifiers).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11100890]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-04-25 18:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found