- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- perl command
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-16-2004 12:33 PM
12-16-2004 12:33 PM
I need the syntax for it.
maximum points for all correct replies.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-16-2004 01:15 PM
12-16-2004 01:15 PM
Re: perl command
\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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-16-2004 06:01 PM
12-16-2004 06:01 PM
Re: perl command
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' ]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-16-2004 07:51 PM
12-16-2004 07:51 PM
Re: perl command
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-16-2004 08:05 PM
12-16-2004 08:05 PM
Re: perl command
^[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.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-16-2004 08:22 PM
12-16-2004 08:22 PM
Re: perl command
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-16-2004 09:02 PM
12-16-2004 09:02 PM
Re: perl command
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-16-2004 09:09 PM
12-16-2004 09:09 PM
Solutionls -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.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-16-2004 09:13 PM
12-16-2004 09:13 PM
Re: perl command
# 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-16-2004 09:53 PM
12-16-2004 09:53 PM
Re: perl command
(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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-20-2004 01:48 PM
12-20-2004 01:48 PM
Re: perl command
It works! Many thanks to you.