1
我新的Perl和我需要寫一個perl腳本符合下列要求動態輸入一個值從CSV陣列(列):新到perl - 用perl
- 需要閱讀csv文件
- 商店中的陣列中的列
- 假設有7個字段(列)中的CSV:字段1,字段2,字段3,字段4,區域5,區域6,場7
我需要能夠給任何領域作爲輸入參數動態。假設如果我給輸入參數字段3,字段7和在CSV的數據是:
No of orders'|Date'|Year'|Exp_date'|Month'|time'|Committed
12'|12122002'|2013'|02022012'|12'|1230'|Yes
然後我想輸出是:
Year~Committed
2013~Yes
和剩餘的列也將在相同的格式:
No of orders~Date~Exp_date~Month~time
12~12122002~02022012~12~1230
目前我從網上一個Perl腳本,提供了我的左片面結果只在硬編碼格式。但我想在運行時給出輸入並希望生成結果。幫助表示感謝。
$filename = 'xyz.csv';
# use the perl open function to open the file
open(FILE, $filename) or die "Could not read from $filename, program halting.";
# loop through each line in the file
# with the typical "perl file while" loop
while(<FILE>)
{
chomp;
# read the fields in the current line into an array
@fields = split('\`\|', $_);
# print the concat
print "$fields[0]~$fields[1]\n";
}
close FILE;
不要試圖用正則表達式解析CSV。改用'Text :: CSV'。 – innaM
但即使我使用Text :: csv,是否可以將列作爲輸入參數? – jesse
'Text :: CSV'逐行解析CSV文件,爲您提供一組字段。在這個腳本中,它將取代上面的'split()'命令。之後你如何操縱田地取決於你。 –