Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: assigning the maximum of two numbers

by sauoq (Abbot)
on Nov 02, 2005 at 23:25 UTC ( [id://505179]=note: print w/replies, xml ) Need Help??


in reply to assigning the maximum of two numbers

So, can you think of any other ways, and are any of them faster than List::Util?

I think this is elegant...

sub max { $_[ $_[0] < $_[1] ] }
I don't know how it would compare, but I'd expect favorably. I, like Aristotle, am not interested in the efficiency difference enough to benchmark.

By the way, you can use that technique inline and avoid the function call (if you are really trying for efficiency.)

my $max_xy = ($x,$y)[$x<$y]

-sauoq
"My two cents aren't worth a dime.";

Replies are listed 'Best First'.
Re^2: assigning the maximum of two numbers
by xdg (Monsignor) on Nov 03, 2005 at 03:42 UTC
    my $max_xy = ($x,$y)[$x<$y]

    That reminds me about a variation of this gem I seem to remember being in Effective Perl Programming. Not as efficient, but more artistic. If I remember correctly, it's like this:

    my $max_xy = [$x=>$y]->[$x<=$y];

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

      No wonder people hate perl. :-(

      I work with people who would *USE* that, too. :-(

Re^2: assigning the maximum of two numbers
by EvanCarroll (Chaplain) on Nov 03, 2005 at 03:09 UTC
    I just feel compelled to reply:

    my $max_xy = ($x,$y)[$x<$y]

    I have never seen that before. You get +100 Evan points for that show of cunning. I plan to write programs around this in the future. I feel it worthy to note though that you lack a special vital caveat. Your code can be borked if one utilizes $[, ex $[=1, You might want to append it slightly:

    my $max_xy = ($x,$y)[abs( ($x<$y)+$[ )];


    Evan Carroll
    www.EvanCarroll.com


    Grandfather WINS!

    Congratulations on your inabliity to pick up on a joke, your prize.. Amulet of a Sense Of Humor (+5 Sense Of Humor). With the amulet equiped your sense of humor is now: 5

    Congrats again.

      $[ is nasty, nasty, nasty, and is deprecated. If all Perl had to be written on the off chance that $[ may have changed, almost no Perl project would get completed.

      For any new code it should be completely safe to ignore $[. About the only good use I can think of for it is to write nasty obfu code, or maybe for Golf.


      Perl is Huffman encoded by design.
      I saw that used a lot over 15 years ago in BASIC code. BASIC didn't have a ternary conditional operator, but it was possible to emulate it.
      $a = $i > $j ? 6 : 10;
      could be written as the following in BASIC (knowing that false is 0 and true is -1):
      LET a = (i > j) * 4 + 10;
      just as it could be written as the following in Perl (knowing that false is 0 and true is 1):
      $a = ($i > $j) * -4 + 10;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-03-28 06:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found