我对 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。我试图让我的表格和之前代码的结果出现在我的输出文件中。我得到了一个好的文件名,但没有结果。
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;
我觉得有一种简单的方法可以做到这一点,也有一种艰难的方式,我采取了非常艰难的方式。任何帮助将非常感激。