<?xml version="1.0" encoding="windows-1252"?>
<node id="828608" title="Re: how to check if a number falls in a range?" created="2010-03-14 16:23:20" updated="2010-03-14 16:23:20">
<type id="11">
note</type>
<author id="580097">
almut</author>
<data>
<field name="doctext">
&lt;blockquote&gt;
&lt;i&gt; how to check if a number falls in a range? &lt;/i&gt;
&lt;/blockquote&gt;
&lt;c&gt;
#!/usr/bin/perl
use strict;
use warnings;

my ($lower, $upper) = (40, 100);

for my $num (17,42,99,111) {

    my $is_between = (sort {$a &lt;=&gt; $b} $lower, $upper, $num)[1] == $num;
    
    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
&lt;/c&gt;
&lt;br /&gt;
&lt;p&gt; Or&amp;nbsp; (not sure which you meant) : &lt;/p&gt;
&lt;blockquote&gt;
&lt;i&gt; how should I check if the $start and $end in the range of $startPosition and $endPosition? &lt;/i&gt;
&lt;/blockquote&gt;
&lt;c&gt;
#!/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 &lt;=&gt; $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]
&lt;/c&gt;
&lt;p&gt; &lt;b&gt;;-)&lt;/b&gt; &lt;/p&gt;</field>
<field name="root_node">
828594</field>
<field name="parent_node">
828594</field>
</data>
</node>
