string 在 Perl 中识别空字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4490166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Identify empty string in Perl
提问by Nate the Noob
There is probably an easy solution to this, but I can't figure it out. I am looking to:
对此可能有一个简单的解决方案,但我无法弄清楚。我期待:
- take a CSV file into an array
- loop through the array and split fields into variables
- if the array field is empty then set the variable to "N/A"
- 将一个 CSV 文件放入一个数组中
- 遍历数组并将字段拆分为变量
- 如果数组字段为空,则将变量设置为“N/A”
Note: It is only setting the $variable
to "N/A
" that I cannot get working.
注意:仅将 设置$variable
为“ N/A
”,我无法开始工作。
For example:
例如:
foreach $var (@list) {
($name,$date,$size, etc...)=split(/,\"/,$var);
}
How would I set $date
to "N/A
" if the field in the array is empty?
如果数组中的字段为空,我将如何设置$date
为“ N/A
”?
so to produce:
所以要生产:
$name = Jim
$date = N/A
$size = small
I hope this makes sense and is easy to fix. -Thanks
我希望这是有道理的,并且很容易解决。-谢谢
回答by moinudin
Assuming the variable $date
is undefined when "empty":
假设变量$date
在“空”时未定义:
if (!defined($date)) {
$date = 'N/A';
}
Or more concisely:
或者更简洁:
$date //= 'N/A';
Or if it really is an empty string, i.e. $date = '';
(this will also work in the case where $date
is undefined, but you don't want to use this if you onlywant to identify the case where it is undefined):
或者,如果它真的是一个空字符串,即$date = '';
(在这种情况下也会工作,其中$date
没有定义,但你不希望使用这个,如果你只是想找出它是未定义的情况下):
if ($date eq '') {
$date = 'N/A';
}
Or more concisely (note that this will also set $date
to N/A
if $date
is '0'
due to Perl's weak typing):
或者更简洁(注意,这也将设置$date
为N/A
if$date
是'0'
由于 Perl 的弱类型):
$date ||= 'N/A';
回答by DVK
As far as your third bullet point and the actual question: to check for emptiness:
For empty string, you can either do the above-mentioned
eq ""
, or you can check the string length:$var = "N/A" unless length($var);
;For an undefined of empty string, in Perl 5.10you can use the "defined-or" (
//
) operator to do the short version:$var = "N/A" unless length($var // '');
In Perl before 5.10 where "defined-or" is not available, you will either have to spell out the defined check:
$var = "N/A" unless defined $var && length($var);
... or, you can just stop caring about undefined warnings by turning them off (h/t brian d foy):
no warnings 'uninitialized'; $_ = "N/A" unless length($_) foreach ($name,$date,$size, etc...); use warnings 'uninitialized'; # Always turn back on.
However, please note that you also should consider a different approach to the first two bullet points. Implementing your own CSV parser which is 100% correct is not trivial - for example, your sample code will break if any of the fields contain a double quote.
Instead, you should always use one of the standard Perl CSV parsers, such as
Text::CSV_XS
.
至于你的第三个要点和实际问题:检查空虚:
对于空字符串,您可以执行上述操作
eq ""
,也可以检查字符串长度:$var = "N/A" unless length($var);
;对于未定义的空字符串,在 Perl 5.10 中,您可以使用“已定义或”(
//
) 运算符来执行简短版本:$var = "N/A" unless length($var // '');
在 5.10 之前的 Perl 中,“defined-or”不可用,您要么必须拼出已定义的检查:
$var = "N/A" unless defined $var && length($var);
...或者,您可以通过关闭它们来停止关心未定义的警告 (h/t brian d foy):
no warnings 'uninitialized'; $_ = "N/A" unless length($_) foreach ($name,$date,$size, etc...); use warnings 'uninitialized'; # Always turn back on.
但是,请注意,您还应该考虑对前两个要点采用不同的方法。实现您自己的 100% 正确的 CSV 解析器并非易事 - 例如,如果任何字段包含双引号,您的示例代码就会中断。
相反,您应该始终使用标准 Perl CSV 解析器之一,例如
Text::CSV_XS
.
回答by StanislawSwierc
$name = "N/A" if (!defined($name) || ($name eq ""))
$date = "N/A" if (!defined($date) || ($date eq ""))
$size = "N/A" if (!defined($size) || ($size eq ""))
Make sure you are using string comparison for comparing strings :)
确保您使用字符串比较来比较字符串:)
回答by nmdr
What will be the input like if date is missing? If the input is: somename,200 (where 200 is size), then date would be set as 200 right?
如果缺少日期,输入会怎样?如果输入是:somename,200(其中 200 是大小),那么日期将设置为 200 对吗?
If the input is like this somename,,200
如果输入是这样的 somename,,200
where 200 is size, and because date is unavailable it is set to empty. Then you can do a simple if-check:
其中 200 是大小,由于日期不可用,所以它被设置为空。然后你可以做一个简单的 if-check:
if($date eq '')
{
$date = "NA";
}
Note $date will be defined, it will be just set to empty
注意 $date 将被定义,它将被设置为空
回答by dhana govindarajan
if ($date eq '') { print "STRING IS EMPTY\n" } else { Print "STRING IS NOT EMPTY\n";}
we can use the above code to identify the empty string ,and using the regular expression is more efficient. The "=~" operator and using regular expression also we can also this problem.
我们可以使用上面的代码来识别空字符串,使用正则表达式效率更高。“=~”操作符和使用正则表达式我们也可以解决这个问题。