Perl-Sensitive Sunglasses PerlMonks

Is the number before, in or after the interval? using spaceship operator <=>

by rsFalse (Friar)
 on Nov 20, 2014 at 17:58 UTC Need Help??

rsFalse has asked for the wisdom of the Perl Monks concerning the following question:

I had a task for myself: given interval and a number, find if it is before, in, or after an interval? It can be find using two basic comparisons. But here I tried to use ternary <=>:
use POSIX; while(<DATA>){ chomp; (\$l, \$r)=split/ /; printf "[%2d %2d] ", \$l, \$r; for \$i(-9..10){ \$i or print "|"; printf "%2d ", floor ((\$i - \$l ) / (\$r - \$l +1 )) <=> 0 } print "\n" } __DATA__ 1 4 2 5 3 7 0 3 -1 5 -8 -2
OUTPUT:
[ 1 4] -1 -1 -1 -1 -1 -1 -1 -1 -1 |-1 0 0 0 0 1 1 1 1 1 1 [ 2 5] -1 -1 -1 -1 -1 -1 -1 -1 -1 |-1 -1 0 0 0 0 1 1 1 1 1 [ 3 7] -1 -1 -1 -1 -1 -1 -1 -1 -1 |-1 -1 -1 0 0 0 0 0 1 1 1 [ 0 3] -1 -1 -1 -1 -1 -1 -1 -1 -1 | 0 0 0 0 1 1 1 1 1 1 1 [-1 5] -1 -1 -1 -1 -1 -1 -1 -1 0 | 0 0 0 0 0 0 1 1 1 1 1 [-8 -2] -1 0 0 0 0 0 0 0 1 | 1 1 1 1 1 1 1 1 1 1 1
For example I can use the answer as the index of list of 3 elements:
qw(in after before)[find(\$left, \$right, \$n)]

Replies are listed 'Best First'.
Re: Is the number before, in or after the interval? using spaceship operator <=>
by GrandFather (Sage) on Nov 20, 2014 at 20:16 UTC

So what is the question?

Perl is the programming world's equivalent of English
Re: Is the number before, in or after the interval? using spaceship operator <=>
by LanX (Archbishop) on Nov 21, 2014 at 02:37 UTC
already combining (\$_ <=> \$l) + (\$_ <=> \$r) gives you more details (abs == 1 when border hit) and you can easily derive any info needed.

DB<62> sub tst { print "\$_: ", (\$_ <=> \$l) + (\$_ <=> \$r), "\n" } DB<63> (\$l,\$r)=(1,4) => (1, 4) DB<64> tst() for 0..6 0: -2 1: -1 2: 0 3: 0 4: 1 5: 2 6: 2

if you need to ignore borders try -(\$_ < \$l) || (\$_ > \$r)

DB<79> sub leg { print "\$_: ", -(\$_ < \$l) || (\$_ > \$r) || 0, "\n" } DB<80> leg() for 0..6 0: -1 1: 0 2: 0 3: 0 4: 0 5: 1 6: 1

Cheers Rolf

(addicted to the Perl Programming Language and ☆☆☆☆ :)

Re: Is the number before, in or after the interval? using spaceship operator <=>
by Anonymous Monk on Nov 21, 2014 at 02:42 UTC
Whatever the question was, here's an answer:
use constant ST => ('-1', ' =', ' 0', ' =', ' 1'); printf ' ' x 8 . "%s\n", join ' ', map sprintf('%2d', \$_), -9 .. 10; while (<DATA>) { my (\$l, \$r) = split; printf "[%2d %2d] %s\n", \$l, \$r, join ' ', map { (ST)[ (\$_ <=> \$l) + (\$_ <=> \$r) + 2 ] } -9 .. 10; } __DATA__ 1 4 2 5 3 7 0 3 -1 5 -8 -2
Output:
-9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 [ 1 4] -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 = 0 0 = 1 1 1 1 1 1 [ 2 5] -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 = 0 0 = 1 1 1 1 1 [ 3 7] -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 = 0 0 0 = 1 1 1 [ 0 3] -1 -1 -1 -1 -1 -1 -1 -1 -1 = 0 0 = 1 1 1 1 1 1 1 [-1 5] -1 -1 -1 -1 -1 -1 -1 -1 = 0 0 0 0 0 = 1 1 1 1 1 [-8 -2] -1 = 0 0 0 0 0 = 1 1 1 1 1 1 1 1 1 1 1 1
I don't see why 0 should not be legal. <=> is not a ternary operator.
Re: Is the number before, in or after the interval? using spaceship operator <=>
by LanX (Archbishop) on Nov 21, 2014 at 00:44 UTC
> It can be find using two basic comparisons.

Strange, I count 6 operations! :)

floor ((\$i - \$l ) / (\$r - \$l +1 )) <=> 0 1 2 3 4 5 6

Cheers Rolf

(addicted to the Perl Programming Language and ☆☆☆☆ :)

PS: Please don't post such stuff in SOPW without a clear question, you are keeping others busy moderating that stuff:

• Tricks belong to Cool Uses For Perl
• Opinions belong to Meditations
Hm.. yes.
And that is simpler solution with two basic comparisons:
map { ("-1", " 0", " 1")[ (\$_ >= \$l) + (\$_ > \$r) ] } -9 .. 10;
(\$_ > \$r) - (\$_ < \$l)

Should already do (can't test on Android :)

update

tested! =)

DB<41> sub tst { (\$_ > \$r) - (\$_ < \$l) } DB<42> \$l=3;\$r=6 => 6 DB<43> print tst(),"\t" for 0..9 => "" -1 -1 -1 0 0 0 0 1 1 1

Cheers Rolf

(addicted to the Perl Programming Language and ☆☆☆☆ :)

Create A New User
Node Status?
node history
Node Type: perlquestion [id://1107946]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2019-12-14 18:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?

No recent polls found

Notices?