我对Perl进程非常陌生。我非常喜欢学习曲线和Perl,但我感到非常沮丧,并且在一项任务上花费了很多很多时间,几乎没有结果。
#!/usr/bin/perl
use strict;
print "Average value of retroviruses for the length of each genome and each of the genes:\n"; #create a title for the script
my $infile = "Lab1_table.txt"; # This is the file path.
open INFILE, $infile or die "Can't open $infile: $!"; # Provides an error message if the file can'tbe found.
# set my initial values.
my $tally = 0;
my @header = ();
my @averages = ();
# create my first loop to run through the file by line.
while (my $line = <INFILE>){
chomp $line;
print "$line\n";
# add one to the loop and essentially remove the header line of value.
# the first line is what was preventing me from caclulating averages as Perl can't calculate words.
my @row = split /\t/, $line; # split the file by tab characters.
$tally++; #adds one to the tally.
if ( $tally == 1 ) { #if the tally = 1 the row is determined as a the header.
@header = @row;
}
# if the tally is anything else besides 1 then it will read those rows.
else {
for( my $i = 1; $i < scalar @row; $i++ ) {
$averages[$i] += $row[$i];
}
foreach my $element (@row){
}
foreach my $i (0..4){
$averages[$i] = $averages[$i] + $row[1..4];
}
}
}
print "Average values of genome, gag, pol and env:\n";
for( my $i = 1; $i < scalar @averages; $i++ ) { # this line is used to determine the averages of the columns and print the values
print $averages[$i]/($tally-1), "\n";
}
所以,我得到的结果是想出我想要的东西(不是我想要的确切格式,但我现在似乎已经接近了)并且他们对列进行平均。
现在的问题是写入一个outfile。我试图让我的表和前面的代码的结果出现在我的outfile中。我得到了一个好的文件名但没有结果。
foreach my $i (1){
my $outfile= "Average_values".".txt";
open OUTFILE, ">$outfile" or die "$outfile: $!";
print "Average values of genome, gag, pol and env:\n";
}
close OUTFILE;
close INFILE;
我觉得有一种简单的方法可以做到这一点而且很艰难,我采取了非常艰难的方式。任何帮助将不胜感激。
答案 0 :(得分:2)
你没告诉Perl在哪里打印:
print OUTFILE "Average values of genome, gag, pol and env:\n";
BTW与use strict
一起,use warnings
。对于处理文件,请使用词法文件句柄和open
的三个参数形式:
open my $FH, '>', $filename or die $!;
print $FH 'Something';
close $FH or die $!;