【问题标题】:How do I decide if a variable is numeric in Perl? [duplicate]如何确定 Perl 中的变量是否为数字? [复制]
【发布时间】:2011-04-17 18:14:03
【问题描述】:

可能重复:
How do I tell if a variable has a numeric value in Perl?

我想确定一个变量(从字符串解析的值)是否为数字。我怎样才能做到这一点?好吧,我想/^[0-9]+$/ 会起作用,但有更优雅的版本吗?

【问题讨论】:

    标签: perl variables numbers


    【解决方案1】:

    您可以使用核心Scalar::Util 模块中的looks_like_number() 函数。
    另请参阅 perlfaq 中的问题:How do I determine whether a scalar is a number/whole/integer/float?

    【讨论】:

    • 这可以说是最正确的答案,因为Scalar::Util 挂钩到 Perl API 以实际询问 Perl 它是否认为变量看起来是数字的。因此,对于避免“数字”警告的常见任务,这将始终有效。当然,有时最好直接说no warnings 'numeric';...
    【解决方案2】:
    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" }
    

    取自这里:http://rosettacode.org/wiki/Determine_if_a_string_is_numeric#Perl

    【讨论】:

    • 如果你坚持使用正则表达式,请从 Regexp::Common 获取它:)
    • 而且,每一个使用 $ 锚点的正则表达式都允许在值的末尾有一个换行符。这是 perlfaq4 中的一个错误(我现在已经修补了文档以消除该错误),其中 Rosetta Code 得到了它的示例。然而,这个例子是要确定它是什么类型的数字,而不是任何类型的数字。
    • 如果我错了,请纠正我 - 但在我看来,在检查严格数字的特殊情况下(即数字的子集,因为数字可以有小数点,也可能有负号甚至是正号,以及逗号或其他一些用于将字符分成三组以进行视觉阅读的视觉分组字符),那么“/\D/”是-其中确实存在未通过仅数字测试的字符串-总是比“/^\d+$/”更有效,因为“/\D/”在扫描第一个非数字字符时为真。
    【解决方案3】:

    使用正则表达式,很好用:

    sub is_int { 
        $str = $_[0]; 
        #trim whitespace both sides
        $str =~ s/^\s+|\s+$//g;          
    
        #Alternatively, to match any float-like numeric, use:
        # m/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/
    
        #flatten to string and match dash or plus and one or more digits
        if ($str =~ /^(\-|\+)?\d+?$/) {
            print "yes  " . $_[0] . "\n";
        }
        else{
            print "no   " . $_[0] . "\n";
        }
    }
    is_int(-12345678901234);     #yes
    is_int(-1);                  #yes
    is_int(23.);                 #yes
    is_int(-23.);                #yes
    is_int(0);                   #yes
    is_int(+1);                  #yes
    is_int(12345678901234);      #yes
    is_int("\t23");              #yes
    is_int("23\t");              #yes
    is_int("08");                #yes
    is_int("-12345678901234");   #yes
    is_int("-1");                #yes
    is_int("0");                 #yes
    is_int("+1");                #yes
    is_int("123456789012345");   #yes
    is_int("-");                 #no
    is_int("+");                 #no 
    is_int("yadz");              #no
    is_int("");                  #no
    is_int(undef);               #no
    is_int("- 5");               #no
    is_int("+ -5");              #no
    is_int("23.1234");           #no
    is_int("23.");               #no
    is_int("--1");               #no
    is_int("++1");               #no
    is_int(" 23.5 ");            #no
    is_int(".5");                #no
    is_int(",5");                #no
    is_int("%5");                #no
    is_int("5%");                #no
    

    或者,您可以使用 POSIX。

    use POSIX;
    
    if (isdigit($var)) {
        // integer
    }
    

    【讨论】:

    • 这只是找到数字。它遗漏了那些不仅仅是数字的数字。
    • POSIX 是 perl 核心的一部分吗?
    • perldoc.perl.org/POSIX.html :从 v5.24 开始,此功能已被删除。这与匹配 qr/ ^ [[:digit:]]+ $ 非常相似。
    猜你喜欢
    • 2011-07-13
    • 1970-01-01
    • 2013-11-22
    • 1970-01-01
    • 2010-09-13
    • 1970-01-01
    • 1970-01-01
    • 2014-03-15
    • 2019-01-25
    相关资源
    最近更新 更多