How can I compare a number against a range in bash or Perl?


Question

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

1 is not within 2-5

or

3 is within 2-5

1
4
2/28/2009 11:03:57 PM

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{
  }
}
19
2/28/2009 5:44:27 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon