469,338 Members | 8,167 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,338 developers. It's quick & easy.

How to find if the number exists in a range?

89
Basically, I want to check whether $x is in between $y and $z.

I always compare them with simple if statement as below.

Expand|Select|Wrap|Line Numbers
  1. if(($x > $y) && ($x < $z))
  2.  
Any other way of doing this in a better manner available? Thanks.
Dec 3 '10 #1

✓ answered by numberwhun

That is a pretty acceptable way to do it, except I would change it to be the following:

Expand|Select|Wrap|Line Numbers
  1. if($x > $y && $x < $z)
  2.  
You don't need all the excess parenthesis, just the one set. You are using the logical AND to separate the comparisons.

In case you are wondering if I tested this, here is the code I used to test it:

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3.  
  4. my $x = 4;
  5. my $y = 1;
  6. my $z = 10;
  7.  
  8. if($x > $y && $x < $z){
  9.      print("Yes\n");
  10. }
  11. else{
  12.      print("No\n");
  13. }
  14.  
Regards,

Jeff

5 10399
numberwhun
3,503 Expert Mod 2GB
That is a pretty acceptable way to do it, except I would change it to be the following:

Expand|Select|Wrap|Line Numbers
  1. if($x > $y && $x < $z)
  2.  
You don't need all the excess parenthesis, just the one set. You are using the logical AND to separate the comparisons.

In case you are wondering if I tested this, here is the code I used to test it:

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3.  
  4. my $x = 4;
  5. my $y = 1;
  6. my $z = 10;
  7.  
  8. if($x > $y && $x < $z){
  9.      print("Yes\n");
  10. }
  11. else{
  12.      print("No\n");
  13. }
  14.  
Regards,

Jeff
Dec 3 '10 #2
lilly07
89
Thanks Jeff.
Dec 3 '10 #3
numberwhun
3,503 Expert Mod 2GB
Your very welcome! Happy Holidays!
Dec 3 '10 #4
toolic
70 Expert
Use the between function from Acme::Tools
Dec 3 '10 #5
numberwhun
3,503 Expert Mod 2GB
Those darn Acme tools. No wonder Wyle E. Coyote loves them so much. :)

I haven't used the Acme packages before. I will have to check this out as well. Hopefully it works for the OP.

Regards,

Jeff
Dec 3 '10 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

9 posts views Thread by Martin Foster | last post: by
1 post views Thread by Nancy Drew | last post: by
3 posts views Thread by FLOTServer | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Marylou17 | last post: by
1 post views Thread by Marylou17 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.