Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

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

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


in reply to Re^2: Why does my get_max_index function return zero? (High Water Mark Algorithm)
in thread Why does my get_max_index function return zero? (High Water Mark Algorithm)

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:

sub get_max_index { my $i, $imax; for ($i=0; $i<@_; $i++) { $imax=$i if ($_[$i] > $_[$imax]); } return $imax; } my @arr = (1..10); my $ans = get_max_index(@arr);

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://11100904]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-24 17:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found