Clear questions and runnable code get the best and fastest answer |
|
PerlMonks |
Re: Trudging along the learning perl path.by Marshall (Canon) |
on Apr 16, 2017 at 23:40 UTC ( [id://1188071]=note: print w/replies, xml ) | Need Help?? |
I already posted an example of another way to do this subroutine at Re: Trudging along the learning perl path.. Below, I have modified your code which uses array indices and "looks forward" to the next element. I am sure you wondered why you needed to sort the array yet again? That is because the index of -1 means the last element of the array in Perl and that caused the largest value to appear at the beginning of the @unique array. Here is another way of using "look ahead" indicies.... compare with your code... I encourage you to sign up for a Perl Monk logon. You are trying to learn and are writing code. Beginners don't write perfect code and by the way sometimes experts don't either! Update: There is a problem with the above code if say, there is only a single element in @arr, i.e., @arr=(29);. This is one of several reasons why I would discourage iterating over array indices in Perl code.
In Section
Seekers of Perl Wisdom
|
|