Perl:由于重新启动在foreach冗余? [英] Perl: redundancy due to restart in foreach?

查看:114
本文介绍了Perl:由于重新启动在foreach冗余?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从2000个子目录中的pairsAngles.txt中grep字符串Distance:;子目录的名称是从csv文件中获得的。但由于某种原因,foreach()从每个循环开始重新开始。所以输出如下所示:

显然所有的距离应该是在一列...

我不确定哪一步导致问题。
代码如下:

$ p $ #!/ usr / bin / perl -w
use strict;
使用警告;
使用File :: Find;
使用List :: MoreUtils qw(uniq);
使用Cwd qw(cwd);
使用Text :: CSV_XS;
使用Data :: Dumper;

my @ pairs = qw();
my @ result =();

my $ in;
my $ out;
my $ pairs;
my $ dist =;
my $ dir =/home/avabelieve/aaPROJECT/helicalPair_ax/selectedPairs/renumberedPdb/clusterPairs-1.25-12-05_windows.12.resle3.2A.RMSD1.3/oligomerAngle;

my $ cluster =clst1.csv;
打开($ in,$ cluster)||死无法打开\$ cluster \:$!;

my $ cU =clst1Updated.csv;
打开($ out,> $ cU)||死无法打开'$ CU'$!;
$ b $ my $ csv = Text :: CSV_XS-> new({binary => 1,auto_diag => 1,eol => $ /});

while(my $ c1 =< $ in>){
chomp $ c1;
推@pairs,$ c1;

foreach $ c1(uniq @pairs){
find(\& Matches,$ dir / $ c1);
子匹配{
打开($ pairs,pairsAngles.txt)或死$!; ($ dist =< $ pair>){

if($ dist =〜m / Distance:/){

chomp $ dist;
push(@result,$ dist\\\
);
@result = split\t,$ dist;
}

}
}
}
chdir..;

if(not $ csv-> eof){
$ csv-> error_diag();
}
$ csv-> say($ out,[uniq @pairs,@result]);
}
关闭$ out或死掉$!;


解决方案

while循环添加到<$ c $($ $> $<
chomp $ c1;
推@pairs,$ c1;



foreach 循环遍历这些对。

  foreach $ c1(uniq @pairs){
find(\& Matches,$ dir / $ C1\" );

由于 foreach 循环位于 while while 循环,每次将一对添加到 @pairs foreach 循环将从头开始重复遍历不断增长的 @pairs

,完成 @pairs 然后循环。

  while( my $ c1 =< $ in>){
chomp $ c1;
推@pairs,$ c1;


foreach $ c1(uniq @pairs){
find(\& Matches,$ dir / $ c1);
...
}

顺便说一下,循环可以更好地编写来利用 chomp 在列表上工作。

  my @pairs =< $ in>; 
chomp @pairs;


I was trying to grep string "Distance: " from "pairsAngles.txt" within each of over 2,000 subdirectories; the names of the subdirectories are obtained from a csv file. But for some reason foreach() restarts from the beginning every loop. So the output looks like this: Apparently all the Distances are supposed to be in one column...

I'm not sure which step is causing the problem. Code as follows:

#!/usr/bin/perl -w
use strict;
use warnings;
use File::Find;
use List::MoreUtils qw(uniq);
use Cwd qw(cwd);
use Text::CSV_XS;
use Data::Dumper;

my @pairs=qw();
my @result=();

my $in;
my $out;
my $pairs;
my $dist = "";
my $dir = "/home/avabelieve/aaPROJECT/helicalPair_ax/selectedPairs/renumberedPdb/clusterPairs-1.25-12-05_windows.12.resle3.2A.RMSD1.3/oligomerAngle";

my $cluster = "clst1.csv";
open ($in, $cluster) || die "cannot open \"$cluster\": $!";

my $cU = "clst1Updated.csv";
open ($out, ">$cU") || die "cannot open '$cU' $!";

my $csv = Text::CSV_XS->new ({ binary => 1, auto_diag => 1, eol => $/ });

while (my $c1 = <$in>) {    
    chomp $c1;
    push @pairs, $c1;

    foreach $c1 (uniq @pairs) {
        find (\&Matches, "$dir/$c1");
        sub Matches {
            open ($pairs, "pairsAngles.txt") or die "$!";

            while (my $dist = <$pairs>) {

                if ($dist =~ m/Distance: /) {                    

                    chomp $dist;
                    push (@result, "$dist\n");
                    @result = split "\t", $dist;
                }               

            } 
        }
    }
    chdir "..";

    if (not $csv->eof) {
        $csv->error_diag();
    }
    $csv->say ($out, [uniq @pairs, @result]);
}
close $out or die "$!";

解决方案

The while loop adds to the list of @pairs.

while (my $c1 = <$in>) {    
    chomp $c1;
    push @pairs, $c1;

The foreach loop iterates over those pairs.

foreach $c1 (uniq @pairs) {
    find (\&Matches, "$dir/$c1");

Since the foreach loop is inside the while loop, every time a pair is added to @pairs the foreach loop will iterate over the ever growing @pairs all over again starting from the beginning.

To avoid this, finish building @pairs and then loop over it.

while (my $c1 = <$in>) {    
    chomp $c1;
    push @pairs, $c1;
}

foreach $c1 (uniq @pairs) {
    find (\&Matches, "$dir/$c1");
    ...
}

Incidentally, that while loop can be better written to take advantage of chomp working on a list.

my @pairs = <$in>;
chomp @pairs;

这篇关于Perl:由于重新启动在foreach冗余?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆