如何修复只打印到以前的文件而不是正确文件的代码?

时间:2015-08-24 14:27:25

标签: perl

我创建了一个程序,目的是为数据库中的每个特征创建单独的文件。

问题在于,当我们创建它时,语句就像它不存在一样工作创建新文件,如果它已经将它放在以前的文件中。

我希望将它放在具有该特征的先前文件中。

代码低于

#!/usr/local/bin/perl

use strict;
use warnings;

my @traitarray;
my $traitarray;
my $input;
my %traithash;
my $t_out;
my $TRAIT;
my $SNPS;
my $PVALUE;
my $PUBMEDID;
my $CHR_POS;
my $CHR_ID;

open( $input, "gwas_catalog_v1.0-downloaded_2015-07-31.tsv" ) || die();

while ( <$input> ) {

    @traitarray = split( /\t/ );

    $TRAIT      = $traitarray[7];
    print "$TRAIT\t";

    $SNPS     = $traitarray[21];
    $PVALUE   = $traitarray[27];
    $PUBMEDID = $traitarray[1];
    $CHR_POS  = $traitarray[12];
    $CHR_ID   = $traitarray[11];

    $PVALUE =~ s/^[^E]*E//;
    $TRAIT =~ tr/ /_/;
    $TRAIT =~ tr/:/-/;
    $TRAIT =~ tr/\//-/;
    $TRAIT =~ tr/*/-/;

    print "$TRAIT\t$SNPS\t$PVALUE\n";

    if ( $PVALUE <= -8 ) {
        if ( !exists $traithash{$TRAIT} ) {
            open( $t_out, ">outputFiles/" . $TRAIT . ".txt" ) || die();
            print $t_out "$SNPS\t $traitarray[27] \t $PUBMEDID \t$CHR_POS \t $CHR_ID \t \n";
            $traithash{$TRAIT} = 1;
            push( @traitarray, $TRAIT );
        }
        else {
            print $t_out "$SNPS\t $traitarray[27] \t $PUBMEDID \t$CHR_POS \t $CHR_ID \t \n";
        }
    }
}

foreach ( $traitarray ) {
    close;
}

2 个答案:

答案 0 :(得分:1)

这看起来非常类似于我会写的东西,我怀疑你试图重用以前的问题的答案而不理解它

您的哈希应该包含以$trait的值索引的文件句柄,并且您的代码应该在创建新特征之前检查是否已经为此特征打开了一个文件

我认为这应该对你有用

#!/usr/local/bin/perl

use strict;
use warnings;

open my $input, "gwas_catalog_v1.0-downloaded_2015-07-31.tsv" or die $!;

my %trait_fh;

while ( <$input> ) {

    chomp;
    my @traits = split /\t/;

    my ($pubmedid, $trait, $chr_id, $chr_pos, $snps, $pvalue) = @traits[1,7,11,12,21,27];
    $pvalue =~ s/^[^e]*e//;
    $trait =~ tr| :/*|_-|;

    local ($,, $\) = ("\t", "\n");
    print $trait, $snps, $pvalue;

    if ( $pvalue <= -8 ) {

        my $file = "outputfiles/$trait.txt";
        my $out_fh;

        if ( $out_fh = $trait_fh{$trait} ) {
            print qq{Reusing file "$file"};
        }
        else {
            open $out_fh, '>', $file or die qq{Failed to open "$file" for output: $!};
            print qq{File "$file" created};
            $trait_fh{$trait} = $out_fh;
        }

        print $out_fh $snps, $pvalue, $pubmedid, $chr_pos, $chr_id;
    }
}

答案 1 :(得分:0)

关闭,然后使用&#34;&gt;&gt;&#34;重新打开并添加到文件中这个问题能够得到解决。

#!/usr/local/bin/perl



use strict;
use warnings;

my @traitarray;
my $traitarray;
my $input ;
my %traithash ;
my $traithash;
my $t_out ;
my $TRAIT;
my $SNPS;
my $PVALUE;
my $PUBMEDID;
my $CHR_POS; 
my $CHR_ID;
open ($input, "gwas_catalog_v1.0-downloaded_2015-07-31.tsv") || die () ;

while(<$input>) {
    @traitarray = split (/\t/);
    $TRAIT = $traitarray[7]; 
    $SNPS = $traitarray[21];
    $PVALUE =$traitarray[27];
    $PUBMEDID =$traitarray[1];
    $CHR_POS =$traitarray[12];
    $CHR_ID =$traitarray[11];
    $PVALUE =~ s/^[^E]*E//;
    $TRAIT =~ tr/ /_/ ;
    $TRAIT =~ tr/:/-/ ;
    $TRAIT =~ tr/\//-/ ;
    $TRAIT =~ tr/*/-/ ;

    if ($PVALUE <= -8) {
        if (!exists $traithash {$TRAIT}) {
            open ($t_out, ">outputFiles/".$TRAIT.".txt") || die () ;
            print $t_out "$SNPS\t $traitarray[27] \t $PUBMEDID \t$CHR_POS \t $CHR_ID \t \n"; 
            $traithash {$TRAIT} = 1 ;
            push (@traitarray, $TRAIT);  
            close $t_out; 
    }
        else {
            open ($t_out, ">>outputFiles/".$TRAIT.".txt") || die () ;
            print $t_out "$SNPS\t $traitarray[27] \t $PUBMEDID \t$CHR_POS \t $CHR_ID \t \n";  
            close $t_out;
        }
    }
}