数据是一个表,其中包含第一行和第一列中的名称,因此我不断收到非数字值错误。我想通过使用if($ row [0] ne“retrovirus”)找出如何忽略第一行,但我不知道如何忽略第一列。我是编程新手,很难理解数组以及如何使它们工作。如何将数据拆分为不包含单词的数字列并将它们添加到一起? 这是我到目前为止所做的,并给出了错误的答案。
#!/usr/bin/perl -w
use strict;
# Part A. Computing the average bp length of the virus's
# genomes and each individual gene in the text file.
my $infile = "lab1_table.txt";
open INFILE, $infile or die "$infile: $!";
my @totals = ();
while (my $line = <INFILE>){
chomp $line;
my $total = 0;
my $n = 0;
# Splitting into columns
my @row = split /\t/, $line;
# Working through and adding up each column
foreach my $element (@row) {
# Ignoring first line with headings
if ($row[0] ne "retrovirus" ){
$total = $total + $element;
print "$total \n";
}
}
}
close INFILE;
答案 0 :(得分:0)
如果您完全不关心该行的第一个元素,请使用shift(@row)
在foreach循环之前。或者,如果要保留原始值,可以从第二个到最后一个获取元素:
#!/usr/bin/perl -w
use strict;
# Part A. Computing the average bp length of the virus's
# genomes and each individual gene in the text file.
my $infile = "lab1_table.txt";
open INFILE, $infile or die "$infile: $!";
while (my $line = <INFILE>)
{
chomp $line;
my $total = 0;
# Splitting into columns
my @row = split /\t/, $line;
# Working through and adding up each column
if ($row[0] ne "retrovirus" )
{
map { $total += $_ } @row[1..(scalar(@row) - 1)];
print "$total \n";
}
}
close INFILE;