Your skill will accomplish what the force of many cannot |
|
PerlMonks |
Re: Why does my get_max_index function return zero? (High Water Mark Algorithm)by Marshall (Canon) |
on Jun 04, 2019 at 15:24 UTC ( [id://11100950]=note: print w/replies, xml ) | Need Help?? |
I presume that you have gotten your code working based upon comments so far? If not, then, summarizing... Update: Well, eating some crow...looks like I goofed on the problem requirements. The advice I give here is good, but it is for a different problem! Darn. I re-coding the variant that passes an array reference in a thread further down. It makes the most sense to use an index if the problem requires an index to be returned. I will make a few other comments:
As a further possibility for your study is a version where a reference to @arr is passed to the subroutine. This is faster than the previous because only a single value is passed to the sub instead of all 10 elements. You will note that this version does one more comparison than absolutely necessary. That is due to the way that the loop is initialized. Tradeoffs where a slight performance hit results in much cleaner source code abound. Here is what that looks like: Now of course the "right" way to implement this function is not to implement it at all and use the version in List::Util as pointed out by karlgoethebier. I see some Monks offered sorting solutions. Yes that works but is way less efficient than linearly scanning for the max and/or min (can do both at the same time). I hope that my examples are understandable to you that they help you upon your Perl journey. Even a very small section of code can have a lot of Perl "fine points". Wish you well!
In Section
Seekers of Perl Wisdom
|
|