Solved

detect a scalar

Posted on 2000-02-24
10
323 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
rename outfile before writing 2 77
what are these modules in perl script 4 110
Vb script to unzip a files and rename the files 5 108
make a default program in ubuntu 6 25
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

751 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