如何确定Perl中变量是否具有数字值?

时间:2020-03-05 18:39:58  来源:igfitidea点击:

Perl中是否有一种简单的方法可以让我确定给定变量是否为数字?类似于以下内容:

if (is_number($x))
{ ... }

将是理想的。当然,首选使用不会在使用-w开关时发出警告的技术。

解决方案

回答

不完美,但是我们可以使用正则表达式:

sub isnumber 
{
    shift =~ /^-?\d+\.?\d*$/;
}

回答

通常,数字验证是​​通过正则表达式完成的。此代码将确定某些内容是否为数字,并检查未定义的变量以免引发警告:

sub is_integer {
   defined $_[0] && $_[0] =~ /^[+-]?\d+$/;
}

sub is_float {
   defined $_[0] && $_[0] =~ /^[+-]?\d+(\.\d+)?$/;
}

这是我们应该阅读的一些阅读材料。

回答

我不相信有什么内置功能可以做到。有关此主题的更多信息,请参阅检测数字中的Perlmonks。

回答

在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
}

请注意,非数字变量将被静默转换为0,这可能仍然是我们想要的。

回答

签出CPAN模块Regexp :: Common。我认为它完全可以满足需求并处理所有极端情况(例如,实数,科学计数法等)。例如

use Regexp::Common;
if ($var =~ /$RE{num}{real}/) { print q{a number}; }

回答

使用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

也可以看看:

[perldoc Scalar::Util][1]
[perldoc perlapi][2]

回答

最初的问题是如何判断变量是否为数字,而不是"是否具有数字"。

有一些运算符对数字和字符串操作数具有单独的操作模式,其中"数字"表示最初是数字或者曾经在数字上下文中使用的任何内容(例如,在$ x =" 123"; 0 + $ x,在加法之前,$ x`是一个字符串,之后被视为数字)。

一种说法是:

if ( length( do { no warnings "numeric"; $x & "" } ) ) {
    print "$x is numeric\n";
}

回答

rexep并不完美...这是:

use Try::Tiny;

sub is_numeric {
  my ($x) = @_;
  my $numeric = 1;
  try {
    use warnings FATAL => qw/numeric/;
    0 + $x;
  }
  catch {
    $numeric = 0;
  };
  return $numeric;
}