Solved

detect a scalar

Posted on 2000-02-24
10
291 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 5

Accepted Solution

by:
PC_User321 earned 50 total points
Comment Utility
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
Comment Utility
What about "-3" ?
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 5

Expert Comment

by:PC_User321
Comment Utility
Thanks, ozo.

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

0
 

Author Comment

by:kitwei
Comment Utility
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
Comment Utility
cool, everything I want, and more, thanks!
0
 
LVL 84

Expert Comment

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

Author Comment

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

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

772 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

11 Experts available now in Live!

Get 1:1 Help Now