Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^2: getting the highest value in a simpler way

by uksza (Monsignor)
on Dec 19, 2004 at 02:31 UTC ( #415930=note: print w/ replies, xml ) Need Help??


in reply to Re: getting the highest value in a simpler way
in thread getting the highest value in a simpler way

Hello!

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

Uksza


Comment on Re^2: getting the highest value in a simpler way
Download Code
Re^3: getting the highest value in a simpler way
by BrowserUk (Pope) on Dec 19, 2004 at 03:07 UTC

    [ $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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (8)
As of 2014-12-28 04:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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





    Results (178 votes), past polls