detect a scalar

hi, is there a way to detect a scalar whether it contains numbers or char?

thx

kitweiAsked:
Who is Participating?
 
PC_User321Connect With a Mentor Commented:
Me again.

Correction: The search string for including decimal points should be

   if (($Mystery =~ /^[0-9]*\.?[0-9]*$/) && ($Mystery =~ /[0-9]/)) {
0
 
PC_User321Commented:
if ($Mystery =~ /^[0-9]+$/) {
   print "$Mystery is a number\n";
} else {

   print "$Mystery is not a number\n";
}
0
 
PC_User321Commented:
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
PC_User321Commented:
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
 
ozoCommented:
What about "-3" ?
0
 
PC_User321Commented:
Thanks, ozo.

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

0
 
kitweiAuthor Commented:
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
 
kitweiAuthor Commented:
cool, everything I want, and more, thanks!
0
 
ozoCommented:
More than you wanted?  Does that mean you didn't want to accept "-3" as a number?
0
 
kitweiAuthor Commented:
no, I meant I appreciate all the information, it helps me learn perl
0
All Courses

From novice to tech pro — start learning today.