我对perl非常陌生,并且仍在努力适应它。所以我一直在练习,我在网上找到了这个项目,并且我一直试图弄明白,但我被困住了。 这些是指导:
Print all records that do not list a discoverer in the eighth field.
This file contains lines of 9 items, the first being:
Adrastea XV Jupiter 129000 0.30 0.00 0.00 Jewitt 1979 in alphabetical order by the name of the planet or moon (first field). The text in [] is the corresponding field from the line above.
The fields in this file are:
1. Name of planet or moon [Adrastea]
2. Number of moon or planet (roman numerals) [XV]
3. Name of the abject around which the satellite orbits [Jupiter]
4. Orbital radius (semimajor axis) in kilometers [129000]
5. Orbital period in days [0.30]
6. Orbital inclination in degrees [0.00]
7. Orbital eccentricity [0.00]
8. Discoverer [Jewitt]
9. Year of discovery [1979]
这是我要打印的数据行的示例(即没有发现者命名的地方):
Earth III Sun 149600000 365.26 0.00 0.02 - - # Discoverer not named -> print
我坚持如何搜索文件并查找哪些行没有列出发现者。
到目前为止这是我的代码(我确定我的最后一条if语句是错误的):
#!/usr/bin/perl
use strict;
use warnings;
my $filename = 'solar.txt';
open(my $fh, '<:encoding(UTF -8)', $filename)
or die "could not open file!!!";
#print "$fh";
while ( my $row = <$fh>) {
my ( $planet,
$number_moons,
$obj_orbit,
$orbital_radius,
$orbital_period,
$orbital_inclination,
$orbtial_eccentricity,
$discoverer,
$year
) = split / /, $row;
if( $row !~ $discoverer ){
print "$row";
}
}
答案 0 :(得分:1)
我用Google搜索并找到了您的数据,而您几乎就在那里。但是,您应该在问题中包含要打印的行的示例,例如:
Adrastea XV Jupiter 129000 0.30 0.00 0.00 Jewitt 1979 # Discoverer named -> don't print
Earth III Sun 149600000 365.26 0.00 0.02 - - # Discoverer not named -> print
这样可以更轻松地诊断代码中的问题。试试这个if
声明:
if ($discoverer eq '-'){
print "$row\n";
}