我試圖引號添加到開始並且已經被從CSV文件中讀取一條線的終點,然後分裂,並加入到一個數組的Perl:加入/ Splits-未初始化的變量
a,b,c<br /> x,y,z<br />
並導致:
"a,b,c"
"x,y,z"
我的數據看起來像我的數據是這樣的:
a,b,c<br /> x,y,z<br />
我我們的代碼ING是:
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"
}
是的,就是這樣。 'split'作爲默認參數'$ _'。你需要指定正確的行,你想通過'split'函數來分割。我會在你的情況下做到這一點:'split/delimiter /,$ splitThisLine' – gaussblurinc 2012-08-06 11:24:45
請看我們可以看到一些* real *輸入數據和你想要的相應輸出嗎?您沒有顯示任何包含「
」的內容。 我認爲你讓Perl和C混淆,因爲你似乎認爲你可以通過數組訪問字符串的字符。 發生了什麼是您正在創建一個'@ lines'數組,其中一個元素等於'join'的返回值。 你立即刪除這個元素與調用'移動'與評論*刪除控制字符*,因此數組結束爲空 – Borodin 2012-08-06 12:00:22
我已經添加了一個數據的例子,當然shit(@lines)刪除第一個元素數組,在我的情況下是一個控制字符。 – QuinsUK 2012-08-06 12:46:56