fix*_*xer 2 floating-point perl string-parsing
是否有任何函数int()可以将字符串转换为浮点值?我目前正在使用以下代码:
$input=int(substr($line,1,index($line,",")-1));
Run Code Online (Sandbox Code Playgroud)
我需要将返回的字符串转换substr为float.
sle*_*man 17
只是使用它.在Perl中,一个看起来像数字的字符串是一个数字.
现在,如果你想在使用之前确定该东西是一个数字,那么就有一个实用方法Scalar::Util:
use Scalar::Util qw/looks_like_number/;
$input=substr($line,1,index($line,",")-1);
if (looks_like_number($input)) {
$input += 1; # use it as a number!
}
Run Code Online (Sandbox Code Playgroud)
根据您在评论中留下的示例输入,提取数字的更健壮的方法是:
$line =~ /([^\[\],]+)/; # <-- match anything not square brackets or commas
$input = $1; # <-- extract match
Run Code Online (Sandbox Code Playgroud)