1826122 Members
4722 Online
109690 Solutions
New Discussion

perl command

 
SOLVED
Go to solution
Pando
Regular Advisor

perl command

Is there a perl command to distinguish a data between a numeric or alphanumeric?

I need the syntax for it.

maximum points for all correct replies.
10 REPLIES 10
Ermin Borovac
Honored Contributor

Re: perl command

\w = matches alphanumeric character plus "_"
\d = matches a digit character

if ($var =~ /^\d+$/) {
print "$var is numeric\n";
} elsif ($var =~ /^\w+$/) {
print "$var is alphanumeric\n";
}

Check perlre(1) for more details.
H.Merijn Brand (procura
Honored Contributor

Re: perl command

# man perlre:

Because patterns are processed as double quoted strings,
the following also work:

\t tab (HT, TAB)
\n newline (LF, NL)
\r return (CR)
\f form feed (FF)
\a alarm (bell) (BEL)
\e escape (think troff) (ESC)
\033 octal char (think of a PDP-11)
\x1B hex char
\x{263a} wide hex char (Unicode SMILEY)
\c[ control char
\N{name} named char
\l lowercase next char (think vi)
\u uppercase next char (think vi)
\L lowercase till \E (think vi)
\U uppercase till \E (think vi)
\E end case modification (think vi)
\Q quote (disable) pattern metacharacters till \E


In addition, Perl defines the following:

\w Match a "word" character (alphanumeric plus "_")
\W Match a non-"word" character
\s Match a whitespace character
\S Match a non-whitespace character
\d Match a digit character
\D Match a non-digit character
\pP Match P, named property. Use \p{Prop} for longer names.
\PP Match non-P
\X Match eXtended Unicode "combining character sequence",
equivalent to (?:\PM\pM*)
\C Match a single C char (octet) even under Unicode.
NOTE: breaks up characters into their UTF-8 bytes,
so you may end up with malformed pieces of UTF-8.
Unsupported in lookbehind.


Enjoy, Have FUN! H.Merijn [ suggesting to also read 'man perlretut' ]
Enjoy, Have FUN! H.Merijn
Pando
Regular Advisor

Re: perl command

Hi procura,

I am very new to perl. Am currently creating a script to test the value of a $Var (please see below). I have used the awk like this in my script
...
...
Var=$(awk -F "," '/DIFFLOTID/ {print $2}' $FILE)

Is it possible for a perl to distinguish the value of $Var if it is numeric or alphanumeric? I need the way to test it.
Many thanks.

Fred Ruffet
Honored Contributor

Re: perl command

Consider a numeric as some numerics followed by a dot and some more numerics. regexp is as follow :
^[0-9][0-9]*[\.[0-9][0-9]*]{0,1}$

so if your var is $Var following perl code whould be OK :
$Var=~ m/^[0-9][0-9]*[\.[0-9][0-9]*]{0,1}$/g;
if (pos($Var)) {
print "$Var Is a numeric.";
} else {
print "$Var Is NOT a numeric.";
}

Regards,

Fred
--

"Reality is just a point of view." (P. K. D.)
Pando
Regular Advisor

Re: perl command

Hi All,

Attached is a portion of the script am making.
I have also attached 2 files for testing.
I need your expertise to test the value of the variable if it is numeric or alphanumeric.
Many thanks to all.


H.Merijn Brand (procura
Honored Contributor

Re: perl command

# man perlfaq4

or

# perldoc -q number/whole


How do I determine whether a scalar is a number/whole/integer/float?

Assuming that you don't care about IEEE notations like
"NaN" or "Infinity", you probably just want to use a regular expression.

if (/\D/) { print "has nondigits\n" }
if (/^\d+$/) { print "is a whole number\n" }
if (/^-?\d+$/) { print "is an integer\n" }
if (/^[+-]?\d+$/) { print "is a +/- integer\n" }
if (/^-?\d+\.?\d*$/) { print "is a real number\n" }
if (/^-?(?:\d+(?:\.\d*)?|\.\d+)$/) { print "is a decimal number\n" }
if (/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/)
{ print "a C float\n" }

There are also some commonly used modules for the task.
Scalar::Util (distributed with 5.8) provides access to
perl's internal function "looks_like_number" for determin�
ing whether a variable looks like a number. Data::Types
exports functions that validate data types using both the
above and other regular expressions. Thirdly, there is
"Regexp::Common" which has regular expressions to match
various types of numbers. Those three modules are avail�
able from the CPAN.

If you're on a POSIX system, Perl supports the
"POSIX::strtod" function. Its semantics are somewhat cum�
bersome, so here's a "getnum" wrapper function for more
convenient access. This function takes a string and
returns the number it found, or "undef" for input that
isn't a C float. The "is_numeric" function is a front end
to "getnum" if you just want to say, ``Is this a float?''


sub getnum {
use POSIX qw(strtod);
my $str = shift;
$str =~ s/^\s+//;
$str =~ s/\s+$//;
$! = 0;
my($num, $unparsed) = strtod($str);
if (($str eq '') || ($unparsed != 0) || $!) {
return undef;
} else {
return $num;
}
}

sub is_numeric { defined getnum($_[0]) }

Or you could check out the String::Scanf module on the
CPAN instead. The POSIX module (part of the standard Perl
distribution) provides the "strtod" and "strtol" for con�
verting strings to double and longs, respectively.

Enjoy, Have F
Enjoy, Have FUN! H.Merijn
Fred Ruffet
Honored Contributor
Solution

Re: perl command

I modified your program this way :
ls -l *.TDF |awk '{print $9}' |while read FILE
do
export Var=$(awk -F "," '/DIFFLOTID/ {print $2}' $FILE)
perl -e '$PerlVar=$ENV{Var};$PerlVar=~ m/^[0-9][0-9]*[\.[0-9][0-9]*]{0,1}$/g;if (pos($PerlVar)) {print "$PerlVar Is a numeric.\n"} else {print
"$PerlVar Is NOT a numeric.\n"}'
done

This integrates my precedent post.
Output is like this :
./test
8964 Is a numeric.
8963ver Is NOT a numeric.

isn't it what you want ?

Regards,

Fred
--

"Reality is just a point of view." (P. K. D.)
H.Merijn Brand (procura
Honored Contributor

Re: perl command

If it's integers only, there is a very dirty way to do it:

# perl -le'$x = $ENV{VAR}; $x += 0; print $x eq $ENV{VAR} ? "OK" : "Not OK"'

This uses automatic conversions from string to numeric, and if the numeric to string conversion equals the original string all conversions are OK

Enjoy, Have FUN! H.merijn
Enjoy, Have FUN! H.Merijn
Ralph Grothe
Honored Contributor

Re: perl command

Unlike "compiling programming languages" perl isn't typed.
(this may however change with the advent of Perl 6 and the Parrot engine, but I'm not into this to tell)
Therefore Perl "decides" on the context, as
procura demonstrated in his last code example.
Although he said that's a dirty hack, this is, I would say, the usual way Perl programmers do it;
i.e. forcing a variable into a explicit context to make Perl treat it as either numeric or string.
If you're interested in the Perl innards and data types in Perl have a look at
perldoc perlguts
though I would assume that's a bit heavy reading for now (advanced stuff).
I would suggest as most others replied to simply stick to regexps and use \d or \w to decide.
Madness, thy name is system administration
Pando
Regular Advisor

Re: perl command

Hello Fred Ruffet!,
It works! Many thanks to you.