Identify empty string in Perl
There is probably an easy solution to this, but I ca开发者_开发技巧n'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"
Note: It is only setting the $variable
to "N/A
" that I cannot get working.
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?
so to produce:
$name = Jim
$date = N/A
$size = small
I hope this makes sense and is easy to fix. -Thanks
Assuming the variable $date
is undefined when "empty":
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 only want to identify the case where it is undefined):
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';
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.10 you 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
.
$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 :)
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?
If the input is like this somename,,200
where 200 is size, and because date is unavailable it is set to empty. Then you can do a simple if-check:
if($date eq '')
{
$date = "NA";
}
Note $date will be defined, it will be just set to empty
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.
精彩评论