473,382 Members | 1,204 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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 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
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,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

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

Similar topics

9
by: Martin Foster | last post by:
Hi. I would like to be able to mimic the unix tool 'uniq' within a Perl script. I have a file with entries that look like this 4 10 21 37 58 83 111 145 184 226...
1
by: R D | last post by:
Thanks for quick reply. Cwd didn't work because it gives what directory you are currently in not the directory the script is in! Can't get FindBin to work..any idea what's wrong with the...
1
by: Nancy Drew | last post by:
hi all it's been years since i've worked with perl, so i'd rather use prefad code. i've got a client who is concerned that a perl http client could login to his server and query a set of forms,...
3
by: FLOTServer | last post by:
Here's my problem: I run a gameserver that runs the game "Medal of Honor". On the game server is log file which contains all of the data from the players for that day (kills, deaths, etc...). I...
3
by: PzYon | last post by:
Hey 2gether! I'm trying to execute a PERL script on the web server when the user presses a button in an ASP.NET Web Application using Visual Basic. The most obvious solution for me seemed to be...
2
by: jonathan184 | last post by:
Hi I am having a problme where the results of the sql count is not matching the results of the perl script sql count. The script was working fine up till Wed last week and after that the results...
9
by: 8anos | last post by:
Hello, I am new at the community and newbie at programming :) As you may know rapidshare provides a perl script for linux, to upload files at their servers. You can find the original scripts at...
2
by: IvanIV | last post by:
Hi All, I have trouble with embedding a Perl interpreter into a C program. I would like to replace some subroutine in a Perl script without changing this script from my C program. I need...
24
by: Peter Michaux | last post by:
I have a Perl script that I want to run as a set-user-ID program. Many OSes don't allow scripts run as set-user-ID. To make this script portable, it seems I need to write a C wrapper program that...
1
by: Ormazd | last post by:
Hello, I was wondering if anyone might be able to help me with a little PERL script? I'm very new and I have been given a task to write a simple Perl script that prints out the file names and...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.