Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://3333]=superdoc: 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.

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!


In reply to Re: Why does my get_max_index function return zero? (High Water Mark Algorithm) by Marshall
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-04-20 00:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found