tags:

views:

1572

answers:

6

How to script a comparison of a number against a range?

1 is not within 2-5

or

3 is within 2-5

+9  A: 

In Perl:

if( $x >= lower_limit && $x <= upper_limit ) {
   # $x is in the range
}
else {
   # $x is not in the range
}
Adnan
appreciate the quick replies!
Um ... I think some sigils are missing. Perl probably does the right thing but you might also want to keep in mind that perl uses floating point as default for numbers, so you have the potential for the 0.999999... == 1.0 problem. But I'm an old Fortran programmer, so discount my words accordingly.
jaredor
Recent perls try hard to keep integers stored as such.
ysth
thanks for the edits brian. The invisible $ in perl frequently bites me.
Adnan
Brad Gilbert
@ysth, I believe that if you had "$x = 5;" perl should keep it as an integer if possible, but if you had "$x = (1 / 2) * 10;" I would hope it would be kept as a float. Unless the problem domain is specifically restricted to integers, the floating point issue merits at least a consideration.
jaredor
I almost want to mark this as Perl5, instead of Perl, but it works on Perl6 as well.
Brad Gilbert
+7  A: 

In bash:

$ if [[ 1 -gt 2 && 1 -lt 5 ]]; then echo "true"; fi
$ if [[ 3 -gt 2 && 1 -lt 5 ]]; then echo "true"; fi
true
jcrossley3
sweet! mucho gracias
Just for fun: if seq $lo $hi | grep -w $num; then echo yes indeedy sir; fi
ysth
if (( lower <= x then echo true; fi
Brad Gilbert
+10  A: 

It's even better in Perl6.

Chained comparison operators:

if( 2 <= $x <= 5 ){
}

Smart-match operator:

if( $x ~~ 2..5 ){
}

Junctions:

if( $x ~~ any 2..5 ){
}

Given / When operators:

given( $x ){
  when 2..5 {
  }
  when 6..10 {
  }
  default{
  }
}
Brad Gilbert
So much of Perl 6 looks awesome that I think I can forgive the few things that don't.
Chris Lutz
I think that smart match works in perl510 too?
jettero
Given/When also works in 5.10, it's one of the things that was back-ported to Perl5.
Brad Gilbert
Truly, when Perl 6 arrives it will change the way your grandchildren write code.
j_random_hacker
Since the task can be solved without difficulty in existing Perls, it makes me cringe to see that this answer -- which lists a pile of incredibly marginal improvements from a language that doesn't exist yet -- should be at the top. I'm tempted to -1 this just to reduce the cringe factor.
j_random_hacker
@j_random_hacker: "doesn't exist yet" is out of date FUD. you could say "not yet released", but it sure does exist: http://www.perl.com/download.csp#perl6
ysth
They only seem to be "marginal improvements" because this is a trivial example. Junctions are amazing...
jettero
@jettero: To be clear, I'm not saying that the features themselves are marginal, only that using them to solve this particular problem is.
j_random_hacker
@ysth: Thanks for the pointer. It looks like Perl 6 is further along than I thought. But a look at the Rakudo feature status page: http://www.perlfoundation.org/perl6/index.cgi?rakudo_feature_status tells me things aren't stable enough yet for anything but playing.
j_random_hacker
I didn't intend for this to get more up-votes than any other question, I just wanted to spread the word about Perl6.
Brad Gilbert
+4  A: 

The smart match operator is available in Perl 5.10, too:

if ( $x ~~ [2..5] ) {
    # do something
}
hillu
So is Given/When.
Brad Gilbert
yeah, and any {$_ == $x} 2..5can be done using List::MoreUtils
hillu
or Quantum::Superpositions. but that or L::MU's any are so horrifically expensive that it curdles my prematurely optimizing blood to think of using them for trivial tests.
ysth
A: 

In perl

grep {/^$number$/} (1..25);

will give you a true value if the number is in the range and a false value otherwise.

For example:

[dsm@localhost:~]$ perl -le 'print "has `$ARGV[0]`" if grep {/^$ARGV[0]$/} (1..25)' 4
has `4`
[dsm@localhost:~]$ perl -le 'print "has `$ARGV[0]`" if grep {/^$ARGV[0]$/} (1..25)' 456
[dsm@localhost:~]$
dsm
"will give you a non-empty list" - actually, perl will return true/false when grep is used in scalar context (like in your examples) - and newer perls also short circuit in this case, iirc.
thanks for the correction
dsm
+1  A: 

In bash:

if [[ $(echo {2..5}) =~ $x ]]; then echo true; fi
Dennis Williamson