473,382 Members | 1,204 Online

# 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

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 11226
numberwhun
3,509 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,509 Expert Mod 2GB
Dec 3 '10 #4
toolic
70 Expert
Use the between function from Acme::Tools
Dec 3 '10 #5
numberwhun
3,509 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