【问题标题】:some help on the following perl script以下 perl 脚本的一些帮助
【发布时间】:2013-04-03 17:21:16
【问题描述】:

在合并/连接/组合/绑定等方面需要帮助

  1. 我有几个 ascii 文件,每个文件都定义一个变量,我已将其转换为单列数组

我有许多变量的列数据,所以我需要像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 中做同样的事情,但它会消耗大量时间,因为文件和感兴趣区域的数量太多并且文件也有点大。

【问题讨论】:

  • 假设您在 *nix 环境中使用join 怎么样?另外,file_bio1.txt 和 file_bio2.txt 是从哪里来的?您是否也尝试在不使用反引号的情况下调用 paste
  • 我不太清楚你要做什么?
  • 不,那是打字错误,我的意思是写 file1.txt 等等...当我尝试代码时,它给了我一个错误,在 EOF 之前的任何地方都找不到字符串终止符(第 12 行).将尝试加入
  • 请编辑这个;显示示例输入文件和预期的输出。

标签: perl merge concatenation


【解决方案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`;

【讨论】:

  • 是的,完全正确。此外,这些文件(file1.text 等)是网格栅格数据的 ascii 文件,因此我尝试将每个文件同时粘贴为一列,将这些 ascii 文件转换为单个列。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-14
  • 1970-01-01
  • 2022-06-24
  • 2011-11-22
  • 1970-01-01
相关资源
最近更新 更多