Solved

detect a scalar

Posted on 2000-02-24
10
308 Views
Last Modified: 2010-03-05
hi, is there a way to detect a scalar whether it contains numbers or char?

thx

0
Comment
Question by:kitwei
  • 5
  • 3
  • 2
10 Comments
 
LVL 5

Expert Comment

by:PC_User321
ID: 2556404
if ($Mystery =~ /^[0-9]+$/) {
   print "$Mystery is a number\n";
} else {

   print "$Mystery is not a number\n";
}
0
 
LVL 5

Expert Comment

by:PC_User321
ID: 2556466
If you wanted to include numbers with a decimal point, the test would have to change to
   if (($Mystery =~ /^[0-9]*.[0-9]*$/) && ($Mystery =~ /[0-9]/)) {

The test above checks that the variable contains at least one of the digits 0-9 and at most one decimal point and nothing else.
0
 
LVL 5

Expert Comment

by:PC_User321
ID: 2556495
Here is an interesting example:-

$Var= "20";   # $Var is a string
$Var++;   # $Var is being made to behave like a number

$Var2 = "2" . "1";         # $Var2 is two chars side by side
print "Equal\n" if ($Var2 == $Var);  # Trying to use $Var2 as a _number_

# Result: Prints "Equal"
# Conclusion:  Perl works out whether you want the variable to be used as a number or a character string, and does the right thing.



0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 5

Accepted Solution

by:
PC_User321 earned 50 total points
ID: 2556534
Me again.

Correction: The search string for including decimal points should be

   if (($Mystery =~ /^[0-9]*\.?[0-9]*$/) && ($Mystery =~ /[0-9]/)) {
0
 
LVL 84

Expert Comment

by:ozo
ID: 2556595
What about "-3" ?
0
 
LVL 5

Expert Comment

by:PC_User321
ID: 2556629
Thanks, ozo.

   if (($Mystery =~ /^[-+]?[0-9]*\.?[0-9]*$/) && ($Mystery =~ /[0-9]/)) {
to allow leading sign.

0
 

Author Comment

by:kitwei
ID: 2556853
I don't have access to try the code until monday, and I must try it that day. I promise I will respond.
Thank you very much.

0
 

Author Comment

by:kitwei
ID: 2566633
cool, everything I want, and more, thanks!
0
 
LVL 84

Expert Comment

by:ozo
ID: 2566688
More than you wanted?  Does that mean you didn't want to accept "-3" as a number?
0
 

Author Comment

by:kitwei
ID: 2569537
no, I meant I appreciate all the information, it helps me learn perl
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now