2014-03-12 81 views

回答

0

有一個內置的功能在Perl稱爲索引功能也使用模式匹配像

使用指數:指數($ stringvariable,「字符搜索」); 以確定某個數字是否使用代碼m/\ d/ 如果您想確定某個值是否爲字符串使用m/\ D/ 使用模式匹配技術。

0

Perl的標量是一個字符串,並在同一時間。爲了測試標量是否可被用作一個數沒有任何警告:

use Scalar::Util qw/looks_like_number/; 

my $variable = ...; 
if (not defined $variable) { 
    # it is not usable as either a number or a string, as it is "undef" 
} 
elsif (looks_like_number $variable) { 
    # it is a number, but can also be used as a string 
} 
else { 
    # you can use it as a string 
} 

實際上,故事是更復雜的處理對象位其可以是或可以不是可作爲數字或字符串。此外,looks_like_number可以返回InfinityNaN(不是數字)的真實值,這可能不是您認爲是數字的值。


要測試一個字符串是否包含一些子,你可以使用正則表達式或index功能:

my $haystack = "foo"; 
my $needle = "o"; 
if (0 <= index $haystack, $needle) { 
    # the $haystack contains the $needle 
} 

有些人喜歡等效試驗-1 != index ...代替。