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”
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
$dateis undefined when “empty”:Or more concisely:
Or if it really is an empty string, i.e.
$date = '';(this will also work in the case where$dateis undefined, but you don’t want to use this if you only want to identify the case where it is undefined):Or more concisely (note that this will also set
$datetoN/Aif$dateis'0'due to Perl’s weak typing):