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

Re: Passing logical operators on as content of scalar

by choroba (Bishop)
on Feb 08, 2018 at 19:10 UTC ( #1208740=note: print w/replies, xml ) Need Help??


in reply to Passing logical operators on as content of scalar

You might be interested in the comparison operator <=>:
#!/usr/bin/perl use warnings; use strict; my $number1 = '2011'; my $number2 = '2011'; my $selectioncriterium = '=='; sub smaller { "$_[0] is smaller than $_[1]" } my %dispatch = ( 0 => sub { "$_[0] equals $_[1]" }, 1 => sub { smaller($_[1], $_[0]) }, -1 => sub { smaller($_[0], $_[1]) }, ); print $dispatch{ $number1 <=> $number2 }->($number1, $number2), "\n";
($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Replies are listed 'Best First'.
Re^2: Passing logical operators on as content of scalar
by soonix (Monsignor) on Feb 08, 2018 at 19:59 UTC
    print $number1, " is ", ("less than", "equal to", "greater than")[($number1 <=> $number2) ++ 1], " ", $number2, "\n";
      print $number1, " is ", ("equal to", "greater than", "less than")[$number1 <=> $number2], " ", $number2, "\n";

      No need to add one :)

        I had thought about that, but decided that it would be easier to grasp without negative indices :-)

        Nice trick, but it calls for an explaining comment.

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1208740]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2018-05-21 17:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?