在合并/连接/组合/绑定等方面需要帮助
我有许多变量的列化数据,所以我需要像R
那样执行列绑定并将其作为单个文件。
我可以在R
中执行相同的操作,但文件太多了。能够用一个代码完成它将有助于节省大量时间。
使用以下代码,对perl是新的,需要帮助。
@filenames = ("file1.txt","file2.txt");
open F2, ">file_combined.txt" or die;
for($j = 0; $j< scalar @filenames;$j++){
open F1, $filenames[$j] or die;
for($i=1;$i<=6;$i++){$line=<F1>;}
while($line=<F1>){
chomp $line;
@spl = split '\s+', $line;
for($i=0;$i<scalar @spl;$i++){
print F2 "$spl[$i]\n";
paste "file_bio1.txt","file_bio2.txt"> file_combined.txt;
}
}
close F1;
}
这里的输入文件是栅格的Ascii文本文件。它们看起来像这样
32 12 34 21 32 21 22 23
12 21 32 43 21 32 21 12
上面提到的没有粘贴语法的代码会将这些文件转换为单个列
32
12
34
21
32
21
22
23
12
21
32
43
21
32
21
12
The output should look like this
12 21 32
32 23 23
32 21 32
12 34 12
43 32 32
32 23 23
32 34 21
21 32 23
每列代表一个不同的ascii文件。 我需要大约15个这样的ascii文件到一个数据帧中。我可以在R中做同样的事情,但由于文件和感兴趣区域的数量太多而文件也有点大,所以它会花费很多时间。
答案 0 :(得分:1)
让我们逐步完成你所拥有的......
# files you want to open for reading..
@filenames = ("file1.txt","file2.txt");
# I would use the 3 arg lexical scoped open
# I think you want to open this for 'append' as well
# open($fh, ">>", "file_combined.txt") or die "cannot open";
open F2, ">file_combined.txt" or die;
# @filenames is best thought as a 'list'
# for my $file (@filenames) {
for($j = 0; $j< scalar @filenames;$j++){
# see above example of 'open'
# - $filenames[$j] + $file
open F1, $filenames[$j] or die;
# what are you trying to do here? You're overriding
# $line in the next 'while loop'
for($i=1;$i<=6;$i++){$line=<F1>;}
# while(<$fh1>) {
while($line=<F1>){
chomp $line;
# @spl is short for split?
# give '@spl' list a meaningful name
@spl = split '\s+', $line;
# again, @spl is a list...
# for my $word (@spl) {
for($i=0;$i<scalar @spl;$i++){
# this whole block is a bit confusing.
# 'F2' is 'file_combined.txt'. Then you try and merge
# ( and overwrite the file) with the paste afterwards...
print F2 "$spl[$i]\n";
# is this a 'system call'?
# Missing 'backticks' or 'system'
paste "file_bio1.txt","file_bio2.txt"> file_combined.txt;
}
}
# close $fh1
close F1;
}
# I'm assuming there's a 'close F2' somewhere here..
看起来你正试图这样做:
@filenames = ("file1.txt","file2.txt");
$oufile = "combined_text.txt";
`paste $filenames[0] $filenames[1] > $outfile`;