Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: getting the highest value in a simpler way

by cLive ;-) (Parson)
on Nov 10, 2004 at 22:31 UTC ( #406829=note: print w/ replies, xml ) Need Help??


in reply to getting the highest value in a simpler way

I like this gem (from Effective Perl Programming by Joseph Hall) for determining the highest value of $x and $y:

$highestvalue = [ $x => $y ] -> [ $x <= $y ]

Very pretty :)

cLive ;-)


Comment on Re: getting the highest value in a simpler way
Download Code
Re^2: getting the highest value in a simpler way
by parv (Priest) on Nov 11, 2004 at 04:31 UTC
    ...which is same as $max = $x <= $y ? $y : $x; and w/o dereferencing. (No, i did not benchmark.)
Re^2: getting the highest value in a simpler way
by uksza (Monsignor) on Dec 19, 2004 at 02:31 UTC
    Hello!

    $highestvalue = [ $x => $y ] -> [ $x <= $y ]
    Anybody can explanin me this code? I'm a bit confused ;-)

    Uksza

      [ $x => $y ] is the same as [ $x, $y ] which is the essentially the same as

      my $ary = ( $x, $y ); my $aryRef = \ @ary;

      And $x <= $y is just a boolean expression that will give a false (0) result if $x > $y and true value (1) otherwise.

      $aryRef->[ 0/1 ] will return either $ary[ 0 ] or $ary[ 1 ].

      Putting it all together, you get an expression that will construct an anonymous array containing $x, $y, then dereferences that anonmous array and uses the boolean result of the comparison to select the greater of the two values before assigning it to the scalar.


      Examine what is said, not who speaks.        The end of an era!
      "But you should never overestimate the ingenuity of the sceptics to come up with a counter-argument." -Myles Allen
      "Think for yourself!" - Abigail        "Time is a poor substitute for thought"--theorbtwo         "Efficiency is intelligent laziness." -David Dunham
      "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
        Hey!

        I understand!! ;-)
        So, in exmaple:
        my $x = 5 my $y = 10 my $highestvalue = [ $x => $y ] -> [ $x <= $y ] #we have @aray(5,10) and because (5 <= 10) it returns 1, so $highestvalue = aray[1] thats mean 10 in this example, right?

        Eh, nice trick... ;-)
        Thanks for you reply.
        Uksza
Re^2: getting the highest value in a simpler way
by redEvo (Novice) on Mar 17, 2011 at 20:59 UTC
    I realize this is a very old thread, but I just found it and was wondering about the efficiency of this. I understand you could have a long, complicated expression with no room for if statements, with [ $x => $y ] -> [ $x <= $y ] as just a small portion of that expression and the example of my $highestvalue is just a simple example. However if you were in a loop, and just wanted to keep overwriting $highestvalue every time you find a higher value, wouldn't it be better to write it this way?:
    my $x = 0; foreach $y (@lots_of_values){ $x = $y if $y > $x; } return $x;
    Is that just as efficient as writing it this way?:
    my $x = 0; foreach $y (@lots_of_values){ $x = [ $x => $y ] -> [ $x <= $y ]; } return $x;
    The second way seems overly complicated in this case.
      Is that just as efficient as writing it this way?:

      Actually, yours is close to 10x more efficient. There is no merit to the 'cute' method beyond it cuteness.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        It makes for a damn good interview question. Asking someone to explain it tells you a lot about how deeply they understand several concepts in Perl. So there's another use :D

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2014-12-27 11:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls