是有一个简单的方法在Perl,将请允许我确定如果一个特定变量数字是?沿线的东西:

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

将是理想的。一种技术,不会把警告当 -w 开关正在使用肯定是首选。

有帮助吗?

解决方案

使用 Scalar::Util::looks_like_number() 它利用内部Perl C API looks_like_number()功能,这可能是最有效的方式来做到这一点。注意串"inf"和"无限"被视为数字。

例如:

#!/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

参见:

其他提示

检查出支持的模块 Regexp::共同的.我认为这不正是你需要和处理所有边缘情况下(例如真正的数字,科学符号,等等)。例如

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

原来的问题是如何告诉我们,如果一个变量的数字,不,如果它"数值"。

有几个运营者具有独立的工作模式为数字和字符串的操作数,其"数值"意味着什么,最初的一些或曾经是使用数字方面(例如在 $x = "123"; 0+$x, ,在外, $x 是一串,后来它被认为是数字).

一种方法来说是这样的:

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

一个简单的(和可能简单的)问题的答案 是的内容 $x 数字 如下:

if ($x  eq  $x+0) { .... }

它没有一个文本相比较原始的 $x$x 转换后的数值。

通常数量的验证,是用经常表达方式。这个代码将决定如果有什么数字,以及检查对于不确定的变量来不扔警告:

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

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

这里有些 阅读材料 你应该看看。

不完美,但是可以使用regex:

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

我不相信有什么内做到这一点。超过你曾经想看到关于这个问题,看看 Perlmonks上检测的数值

一个稍微更强大的regex可以发现在 Regexp::共同的.

它听起来像是你要知道,如果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,这可能是你想要什么无论如何。

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

试试这个:

If (($x !~ /\D/) && ($x ne "")) { ... }

我发现这个有趣的不过

if ( $value + 0 eq $value) {
    # A number
    push @args, $value;
} else {
    # A string
    push @args, "'$value'";
}

我个人认为,要走的道路是依靠Perl的内部上下文,以使解决子弹的证据。一个很好的regexp可能匹配的所有有效的数值和没有非数人(或相反),而是作为有一种方法的采用同样的逻辑的解释使用这应该是安全的依赖。

我倾向于我的脚本 -w, 我不得不结合起来的想法比较的结果"的价值加上为零的"的原始价值的 no warnings 基础的方法的@ysth:

do { 
    no warnings "numeric";
    if ($x + 0 ne $x) { return "not numeric"; } else { return "numeric"; }
}

你可以使用普通的表达来确定,如果$foo是一个数量(或不)。

看看这里:我怎么确定是否的一个标是一个数字

如果(定义$x&&$x!~ m/\D/){} 或 $x=0如果!$x;if($x!~ m/\D/){}

这是一个微小的变化Veekay的答案,但让我解释一下我的推理为的改变。

执行regex在一个未定义的价值会引起错误的排放,并会引起码要退出,在许多--如果不是大多数环境。测试,如果价值是限定或设置一个默认的情况下,就像我在替代方案的例子之前运行的表达会,至少,挽救你的错误记录。

这一功能工作对我来说:

    sub IS_Integer() 
    {
        my $Text = shift;
        my $Integer = 0;

        if ($Text =~ /\D/)
        {
            $Integer = 1;
        }
        if ($Text =~ /^\d+$/)
        { 
            $Integer = 1;
        }
        if ($Text =~ /^-?\d+$/)       
        { 
            $Integer = 1;
        }
        if ($Text =~ /^[+-]?\d+$/)    
        { 
            $Integer = 1; 
        }
        if ($Text =~ /^-?\d+\.?\d*$/) 
        {
            $Integer = 1; 
        }
        if ($Text =~ /^-?(?:\d+(?:\.\d*)?&\.\d+)$/) 
        {
            $Integer = 1;
        }
        if ($Text =~ /^([+-]?)(?=\d&\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/)
        {
            $Integer = 1;
        }

        return $Integer;
    }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top