Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

detect a scalar

Posted on 2000-02-24
10
Medium Priority
?
334 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
Technology Partners: 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 200 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 85

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 85

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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
Six Sigma Control Plans

926 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