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

by BhariD (Sexton)
 on Mar 14, 2010 at 17:38 UTC Need Help??
BhariD has asked for the wisdom of the Perl Monks concerning the following question:

I have to check whether a given number falls in the range. I have the following script: I have pasted an example here, I am reading the \$start and \$end number from another file..

```use strict;
use warnings;

#start and end number from another file
my \$start = '20';
my \$end = '100';

#reading another file, to retrieve info based on if the
#\$start and \$end numbers above falls in the range
#specified in a given line..

while (my \$line = <DATA>){
chomp \$line;
my @cols = split(/\t/, \$line);

# the range from a different file
\$startPosition = \$cols[1];
\$endPosition = \$cols[2];

#here how should I check if the \$start and \$end
#in the range of \$startPosition and \$endPosition?
{
#if true print line
print \$line, "\n";
}
}
```<DATA>
CP1006    100    200
CP1006    110    200
CP1006    40    100
SPS008    60    110

Thanks!

Replies are listed 'Best First'.
Re: how to check if a number falls in a range?
by almut (Canon) on Mar 14, 2010 at 20:23 UTC
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]

;-)

Re: how to check if a number falls in a range?
by Corion (Pope) on Mar 14, 2010 at 17:39 UTC

How would you check whether one number is above another? What have you tried? See perlsyn and your course notes.

Re: how to check if a number falls in a range?
by toolic (Bishop) on Mar 14, 2010 at 17:49 UTC

Create A New User
Node Status?
node history
Node Type: perlquestion [id://828594]
Approved by toolic
help
Chatterbox?
 [Discipulus]: ♫ entre nubes de algodon ♫♪ [karlgoethebier]: ♪ ♫ ♬ Alles hat ein Ende nur die Wurst hat zwei ♪ ♫ [Discipulus]: lol karl, i must learn this sentence by heart

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (11)
As of 2017-07-25 09:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
I came, I saw, I ...

Results (369 votes). Check out past polls.