Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: how to check if a number falls in a range?

by almut (Canon)
on Mar 14, 2010 at 20:23 UTC ( #828608=note: print w/replies, xml ) Need Help??


in reply to how to check if a number falls in a range?

how to check if a number falls in a range?
#!/usr/bin/perl use strict; use warnings; my ($lower, $upper) = (40, 100); for my $num (17,42,99,111) { my $is_between = (sort {$a <=> $b} $lower, $upper, $num)[1] == $nu +m; printf "$num is%s between $lower and $upper\n", $is_between ? "" : + " not"; } __END__ 17 is not between 40 and 100 42 is between 40 and 100 99 is between 40 and 100 111 is not between 40 and 100

Or  (not sure which you meant) :

how should I check if the $start and $end in the range of $startPosition and $endPosition?
#!/usr/bin/perl use strict; use warnings; use 5.010; my ($lower, $upper) = (40, 100); for my $range ( [10,17], [30,71], [42,99], [83,120], [101,111] ) { my $is_within = [(sort {$a <=> $b} $lower, $upper, @$range)[1,2]] +~~ $range; printf "[@$range] is%s within [$lower $upper]\n", $is_within ? "" +: " not"; } __END__ [10 17] is not within [40 100] [30 71] is not within [40 100] [42 99] is within [40 100] [83 120] is not within [40 100] [101 111] is not within [40 100]

;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://828608]
help
Chatterbox?
[GotToBTru]: heh
[GotToBTru]: I
[GotToBTru]: ve been looking at a document we have been sending to a customer for years now
[MidLifeXis]: We don't, at least in this case ;-)
[GotToBTru]: and it includes a reference number that I didnt recognize
[GotToBTru]: I had to dig into the code to find out where it came from .. and it makes no sense
[GotToBTru]: so I emailed my contact asking if we could just stop sending it .. I'm afraid she is going to ask "what is that anyway?"

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (9)
As of 2017-01-20 19:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you watch meteor showers?




    Results (176 votes). Check out past polls.