regexperl

How to check a value to be present in specific range?


Is it possible to compare the value of an expression like this in perl?

if(0x100 < $my_var < 0x200) {
    return $my_var;
}

The above way gives a syntax error.

In perl, is there a better way to check if the value is present in specific range?

I don't want to use two if conditions to do this. I am sure, for this, there would a better way in any language.

Sorry if this a very basic question. I am trying to write my first programs in perl.

Edit

if($my_var > 0x100  && $my_var < 0x200) 

I donot like the above method. Please let me know if there is any other sophisticated way. I would be using it a lot on 64 bit addresses. Hence writing like this, would make the code look ugly. Please suggest other ways.


Solution

  • You can write your own function,

    sub between {
      my ($x,$y,$z) = @_;
    
      return ($x < $y and $y < $z);
    }
    
    if (between(0x100, $my_var, 0x200)) { print "OK"; }