我正在尝试在从 CSV 文件中读取的行的开头和结尾添加引号,然后拆分并添加到数组中
a,b,c<br /> x,y,z<br />
并导致:
"a,b,c"
"x,y,z"
我的数据看起来像我的数据看起来像:
a,b,c<br /> x,y,z<br />
我正在使用的代码是:
my @lines = join("\"", split (qr{<br\s?/>})), $line;
我认为这会起作用,但我不断得到:
"Use of uninitialized value $_"
我试图找出如何解决这个问题,我假设它(对于某人)将是我想念的简单的东西。
额外的信息
我知道如果我想在开头和结尾添加引号,我会使用:
push (@lines, "\"");
unshift (@lines, "\"");
my $newStr = join $/, @lines;
print $newStr;
完整的代码是:
use warnings;
use Text::CSV;
use Data::Dumper;
use constant debug => 0;
use Text::CSV;
print "Running CSV editor......\n";
#my $csv = Text::CSV->new({ sep_char => ',' });
my $file = $ARGV[0] or die "Need to get CSV file on the command line\n";
my $fileextension = substr($file, -4);
#If the file is a CSV file then read in the file.
if ($fileextension =~ m/csv/i) {
print "Reading and formating: $ARGV[0] \n";
open(my $data, '<', $file) or die "Could not open '$file' $!\n";
my @fields;
while (my $line = <$data>) {
#Clears the white space at the end of the line.
chomp $line;
#Splits the line up and removes the <br />.
my @lines = join("\"", split (qr{<br\s?/>})), $line;
#Removes the control character.
shift (@lines);
print "\n";
#print $_, $/ for @lines;
}
print "\n Finished reading and formating: $ARGV[0] \n";
}
else {
print "Error: File is not a CSV file\n"
}