在Perl中,是否有一种简单的方法可以让我确定给定变量是否为数字?类似于以下内容:
if (is_number($x))
{ ... }
最理想的是一种在使用-w
开关时不会引发警告的技术,这当然更受欢迎。
使用Scalar::Util::looks_like_number()
函数,它使用内置的Perl C API的looks_like_number()
函数,这可能是最有效的方法。
请注意,字符串"inf"和"infinity"被视为数字。
#!/usr/bin/perl
use warnings;
use strict;
use Scalar::Util qw(looks_like_number);
my @exprs = qw(1 5.25 0.001 1.3e8 foo bar 1dd inf infinity);
foreach my $expr (@exprs) {
print "$expr is", looks_like_number($expr) ? '' : ' not', " a number\n";
}
输出结果如下:
1 is a number
5.25 is a number
0.001 is a number
1.3e8 is a number
foo is not a number
bar is not a number
1dd is not a number
inf is a number
infinity is a number
最初的问题是如何判断一个变量是否为数字,而不是它“是否具有数值”。
有几个运算符对数字和字符串操作数有不同的操作模式,“数字”表示原来是数字或曾在数字上下文中使用过(例如在$x = “123”;0 + $x
中,在加法之前,$x
是一个字符串,之后被认为是数字)。
其中一种方法是:
if ( length( do { no warnings "numeric"; $x & "" } ) ) {
print "$x is numeric\n";
}
如果启用了位运算功能,那么&
就只是一个数值运算符,并添加了一个单独的字符串&.
运算符,您必须禁用它:
if ( length( do { no if $] >= 5.022, "feature", "bitwise"; no warnings "numeric"; $x & "" } ) ) {
print "$x is numeric\n";
}
(位运算符在Perl 5.022及以上版本中可用,并且如果您使用use 5.028;
或更高版本,则默认启用。)
sub numeric {
$obj = shift;
no warnings "numeric";
return eval('length($obj & "")');
}
- yogibimbimy $obj = shift;
。为什么要用eval? - ysthmy $obj = shift
,当然,只是没有正确地从我的代码转移到注释中,我进行了一些编辑。但是,sub numeric { my $obj = shift; no warnings "numeric"; return length($obj & ""); }
产生了相同的错误。当然,有一个秘密全局变量会解释这种行为,这正是我在那种情况下所期望的,但不幸的是,事情并不那么简单。此外,这将被 strict
和 warnings
捕获。我尝试了 eval,这是一个相当绝望的尝试,以摆脱错误,并且它起作用了。没有更深入的推理,只是试错。 - yogibimbisub numeric { my $obj = shift; no warnings "numeric"; return length($obj & ""); }
print numeric("w") . "\n"; #=>0
,
print numeric("x") . "\n"; #=>0
,
print numeric("1") . "\n"; #=>0
,
print numeric(3) . "\n"; #=>1
,
print numeric("w") . "\n"; #=>1
. 如果在长度周围加上 eval(''),则最后一个打印将给出 0,就像它应该的那样。去理解吧。 - yogibimbi请查看CPAN模块Regexp::Common。我认为它可以完全满足您的需求并处理所有边缘情况(例如实数、科学计数法等)。例如:
use Regexp::Common;
if ($var =~ /$RE{num}{real}/) { print q{a number}; }
通常使用正则表达式来进行数字验证。此代码将确定某个东西是否是数字,并检查未定义的变量,以避免抛出警告:
sub is_integer {
defined $_[0] && $_[0] =~ /^[+-]?\d+$/;
}
sub is_float {
defined $_[0] && $_[0] =~ /^[+-]?\d+(\.\d+)?$/;
}
这里有一些阅读资料,你应该看一下。
$x
是否是数值类型的内容的一个简单(也许过于简化)的答案如下:
if ($x eq $x+0) { .... }
它对原始的$x
和将$x
转换为数字值后的值进行文本比较。
$x eq (($x+0)."")
来解决,然而更糟糕的问题是,在这个函数下,“1.0”不是数值。 - Eponymous虽不是完美的方法,但您可以使用正则表达式:
sub isnumber
{
shift =~ /^-?\d+\.?\d*$/;
}
正则表达式并非完美...原因如下:
use Try::Tiny;
sub is_numeric {
my ($x) = @_;
my $numeric = 1;
try {
use warnings FATAL => qw/numeric/;
0 + $x;
}
catch {
$numeric = 0;
};
return $numeric;
}
在Regexp::Common中可以找到一个稍微健壮一些的正则表达式。
看起来你想知道Perl是否认为一个变量是数字。这里有一个函数可以捕获该警告:
sub is_number{
my $n = shift;
my $ret = 1;
$SIG{"__WARN__"} = sub {$ret = 0};
eval { my $x = $n + 1 };
return $ret
}
另一个选择是在本地关闭警告:
{
no warnings "numeric"; # Ignore "isn't numeric" warning
... # Use a variable that might not be numeric
}
我觉得这很有趣。
if ( $value + 0 eq $value) {
# A number
push @args, $value;
} else {
# A string
push @args, "'$value'";
}
试试这个:
If (($x !~ /\D/) && ($x ne "")) { ... }
perldoc perlapi
的路径告诉我们:测试SV的内容是否类似于数字(或者是数字)。即使您的atof()无法理解它们,“Inf”和“Infinity”也被视为数字(因此不会发出非数字警告)。几乎不是可测试的规范... - Day0x12
这样的十六进制字符串在此测试中不被视为数字。 - Adam Katzlooks_like_number
暴露了 内部函数,它与 Perl 用于将字符串神奇地转换为数字的方式相同。下划线、十六进制、八进制和二进制数字仅允许作为源代码中的字面量,而不是字符串,因此例如0+"0x15"
不起作用。(请注意,looks_like_number("0 but true")
是真的! :-) 参考资料) - haukex