Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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]

;-)


In reply to Re: how to check if a number falls in a range? by almut
in thread how to check if a number falls in a range? by BhariD

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (2)
As of 2024-04-20 05:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found