Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
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??


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

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.

sub get_max_index { #revisit how to define functions/"my" var ? (defa +ult parameters?) my $imax=0; for (my $i=0; $i<@_; $i++) { $imax=$_[$i] if ($imax<=$_[$i]);} return $imax; }
I will make a few other comments:
  1. You are using @_ directly. And yes that works, however, in general this is not a good idea. Usually it is better to make a copy of whatever you want from @_ and then use that copy (or those copies) within your sub. @_ is an alias and if you modify that critter in the sub, you are changing the array in the calling program. This can lead to confusion and mistakes. There can be performance reasons to access @_ directly and the List::Util package does so for little functions like this. However, that is like a 0.0001% case.
  2. The use of subscripts to access array elements is just not done in situations like this (entire array accessed sequentially). Perl has wonderful array iterators that avoid the possibility of an "off by one" subscript mistake - one of the if not the most common error in programming.
So having said that, I offer this code:
use strict; use warnings; sub get_max_index { my (@copyOfArray) = @_; #make an explicit local copy my $imax= shift @copyOfArray; # modifies this copy, but ok for (@copyOfArray){ # no indices... $imax = $_ if $imax < $_; } return $imax; } my @arr = (1..10); my $ans = get_max_index(@arr); print"$ans\n";
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:
use strict; use warnings; sub get_max_index { my $arrayRef = shift; my $imax = $arrayRef->[0]; #don't modify @arr for (@$arrayRef){ $imax = $_ if $imax < $_; } return $imax; } my @arr = (1..10); my $ans = get_max_index(\@arr); #pass reference to array! print"$ans\n";
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!

Replies are listed 'Best First'.
Re^2: Why does my get_max_index function return zero? (High Water Mark Algorithm)
by holli (Abbot) on Jun 04, 2019 at 16:59 UTC
    Your code returns 10. It should return 9.


    holli

    You can lead your users to water, but alas, you cannot drown them.
      What?? it is supposed to return the next to the highest? Oh... My gosh.......this thing wants the index of highest value, not the highest value itself!! Should have read more carefully.. That of course changes things Ooops...Do not write code before consuming at least one full cup of coffee!

      Update:
      Ok, if the problem statement requires returning an index, then the most natural formulation would be to use indices. Here is how that looks when a reference to the array is passed to the sub. I just completely blew it and missed that key piece of info, probably because working with an index is a very rare in my coding. More usual in my problem space might be to return a reference to an entire row of a multidimensional array that matches some criteria. additional comment: I remember needing indices when working with some kinds of tk widgets. So a requirement for this sort of thing definitely exists.

      use strict; use warnings; sub get_max_index { my $arrayRef = shift; my $imax = 0; for my $i (0..@$arrayRef-1){ $imax = $i if $arrayRef->[$i] > $arrayRef->[$imax]; } return $imax; } my @arr = (1..10); my $ans = get_max_index(\@arr); #pass reference to array! print"$ans\n";
        Good, now try again using foreach, but without adding a loop counter =) In the meantime I will put on my flip-flops and chill at the lake.


        holli

        You can lead your users to water, but alas, you cannot drown them.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-19 19:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found