- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to check if a variable has a numeric value in Perl?
Suppose we get a variable at runtime in Perl and we want to check if the value that it contains is numeric or not, then we can use the two approaches shown in this tutorial. We will use two simple examples to demonstrate how it works.
Example
The most basic approach is to use the length and do keywords and then ignore the warnings. Consider the code shown below for the same.
$x = 100; if (length(do { no warnings "numeric"; $x & "" })){ print "x is numeric\n"; } else { print "x is non-numeric\n"; }
Output
If you run the above code in a Perl compiler, you will get the following output on the terminal −
x is numeric
Example
As soon as you change the variable "x" to something that is non-numeric, you will get the output of the "else" block in the "if" condition.
$x = 'abc'; if (length(do { no warnings "numeric"; $x & "" })){ print "x is numeric\n"; } else { print "x is non-numeric\n"; }
Output
It will produce the following output −
x is non-numeric
Example
Another approach to check whether a variable is numeric or not is to use the "Scalar::Util::looks_like_number()" API. It uses the internal "looks_like_number()" function of the Perl C API, which is the most efficient method. There is no difference between the strings "inf" and "infinity".
Consider the code shown below −
use warnings; use strict; use Scalar::Util qw(looks_like_number); my @randomStuff = qw(10 15 .25 0.005 1.4e8 delhi India tutorialsPoint inf infinity); foreach my $randomStuff (@randomStuff) { print "$randomStuff is", looks_like_number($randomStuff)? ' ': ' not', " a number\n"; }
Output
If you run this code in a Perl compiler, it will produce the following output on the terminal −
10 is a number 15 is a number .25 is a number0.005 is a number 1.4e8 is a number delhi is not a number India is not a number tutorialsPoint is not a number inf is a number infinity is a number
- Related Articles
- How to check if a Perl array contains a particular value?
- How to check if a "lateInit" variable has been initialized in Kotlin?
- How to check if a file exists in Perl?
- How to check if a matrix has any missing value in R?
- How to check if a variable exists in JavaScript?
- How to check if a data frame has any missing value in R?
- How to check if a Perl hash already contains a key?
- How to check if a variable is NaN in JavaScript?
- How to check if a variable is boolean in JavaScript?
- How to check if input is numeric in C++?
- Swift Program to Check if a String is Numeric
- Java Program to Check if a String is Numeric
- C++ Program to Check if a String is Numeric
- Golang Program to Check if a String is Numeric
- Haskell Program to Check if a String is Numeric
