用perl将两个TXT文件合成一个csv文件?
1个回答
展开全部
举个例子
write_file("t1.txt", "aa\tbb\ncc\tdd\n");
write_file("t2.txt", "mm\tnn\noo\tpp\n");
my $m = read_file('t1.txt');
my $n = read_file('t2.txt');
my $str;
for (0..$#$m) {$str .= join(",", @{$m->[$_]}, @{$n->[$_]}) . "\n";}
write_file('t.csv', $str);
sub write_file {
open (my $h, '+>', shift) || exit;
print $h shift;
close $h;
}
sub read_file {
open (my $h, shift) || exit;
my @arr;
while (<$h>) {
chomp;
push @arr, [split("\t", $_)];
}
return [@arr];
}
追问
大神能不能帮我加点注释呀,刚开始接触Perl,面试要用,急求!谢谢啦
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询