We help IT Professionals succeed at work.

detect a scalar

kitwei
kitwei asked
on
360 Views
Last Modified: 2010-03-05
hi, is there a way to detect a scalar whether it contains numbers or char?

thx

Comment
Watch Question

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

   print "$Mystery is not a number\n";
}
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.
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.



This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
What about "-3" ?
Thanks, ozo.

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

Author

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.

Author

Commented:
cool, everything I want, and more, thanks!
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
More than you wanted?  Does that mean you didn't want to accept "-3" as a number?

Author

Commented:
no, I meant I appreciate all the information, it helps me learn perl

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.