除了像其他人所建議的那樣使用Text :: CSV,爲了好玩,我已經重寫了代碼,因爲我可能已經寫了它。它包含了很多這裏提出的建議以及我的一些個人風格選擇。如果您有任何疑問,請詢問。此外,如果您可以發佈一些示例數據,我可以檢查它是否有效。
#!/usr/bin/env perl
use strict;
use warnings;
my $lowestBirthRates = 100000000;
my $highestBirthRates = 0;
my $filename = 'census2008.txt';
open (my $fh, '<', $filename) or die "Cannot open $filename: $!";
my %data;
while (<$fh>) {
chomp;
my ($sumlev, $stname,$ctyname,$popestimate2008,$births2008,$deaths2008) = split(",");
push (@{$data{SumLev}}, $sumlev);
push (@{$data{StName}}, $stname);
push (@{$data{CtyName}}, $ctyname);
push (@{$data{PopEstimate}}, $popestimate2008);
push (@{$data{Births}}, $births2008);
push (@{$data{Deaths}}, $deaths2008);
}
my $i = 0;
my $size = @{$data{Births}};
while ($i < $size) {
if ($data{SumLev}[$i] eq " 040"){
#if ($data{SumLev}[$i] == 40){
my $temp = $data{Births}[$i]/$data{PopEstimate}[$i]/541;
if($lowestBirthRates > $temp && $temp > 0){
$lowestBirthRates = $temp;
}
if ($highestBirthRates < $temp){
$highestBirthRates = $temp;
}
}
$i++;
}
print <<REPORT;
Lowest birth rate in LOW-STATE: $lowestBirthRates per 541
Highest birth rate in HIGH-STATE: $highestBirthRates per 541
In Washington:
Lowest birth rate in LOW-COUNTY County, WA: x.xxx per 541
Highest birth rate in HIGH-COUNTY County, WA: x.xxx per 541
REPORT
,如果您有有Perl版本5.14或更高版本安裝的奢侈,你可以使用一個更清晰的語法,其中push
可直接乘坐引用,並在each
可以給所關注的索引以及價值。
#!/usr/bin/env perl
use strict;
use warnings;
use 5.14.0;
my $lowestBirthRates = 100000000;
my $highestBirthRates = 0;
my $filename = 'census2008.txt';
open (my $fh, '<', $filename) or die "Cannot open $filename: $!";
my %data = (
SumLev => [],
StName => [],
CtyName => [],
PopEstimate => [],
Births => [],
Deaths => [],
);
while (<$fh>) {
chomp;
my ($sumlev, $stname,$ctyname,$popestimate2008,$births2008,$deaths2008) = split(",");
push ($data{SumLev}, $sumlev);
push ($data{StName}, $stname);
push ($data{CtyName}, $ctyname);
push ($data{PopEstimate}, $popestimate2008);
push ($data{Births}, $births2008);
push ($data{Deaths}, $deaths2008);
}
while (my ($i, $births) = each $data{Births}) {
if ($data{SumLev}[$i] eq " 040"){
#if ($data{SumLev}[$i] == 40){
my $temp = $births/$data{PopEstimate}[$i]/541;
if($lowestBirthRates > $temp && $temp > 0){
$lowestBirthRates = $temp;
}
if ($highestBirthRates < $temp){
$highestBirthRates = $temp;
}
}
}
print <<REPORT;
Lowest birth rate in LOW-STATE: $lowestBirthRates per 541
Highest birth rate in HIGH-STATE: $highestBirthRates per 541
In Washington:
Lowest birth rate in LOW-COUNTY County, WA: x.xxx per 541
Highest birth rate in HIGH-COUNTY County, WA: x.xxx per 541
REPORT
最後這裏是使用Tie::Array::CSV
一個實現我寫信給能夠使用CSV文件,就像在Perl的2D陣列(即ArrayRefs的陣列)。它使用Text::CSV
進行分析,並使用Tie::File
進行線路訪問。這意味着您不需要像前面的例子那樣將所有數據存儲在內存中。
#!/usr/bin/env perl
use strict;
use warnings;
use Tie::Array::CSV;
my $lowestBirthRates = 100000000;
my $highestBirthRates = 0;
my $filename = 'census2008.txt';
tie my @data, 'Tie::Array::CSV', $filename
or die "Cannot tie $filename: $!";
foreach my $row (@data) {
my ($sumlev, $stname, $ctyname, $popest, $births, $deaths) = @$row;
if ($sumlev eq " 040"){
#if ($sumlev == 40){
my $temp = $births/$popest/541;
if($lowestBirthRates > $temp && $temp > 0){
$lowestBirthRates = $temp;
}
if ($highestBirthRates < $temp){
$highestBirthRates = $temp;
}
}
}
print <<REPORT;
Lowest birth rate in LOW-STATE: $lowestBirthRates per 541
Highest birth rate in HIGH-STATE: $highestBirthRates per 541
In Washington:
Lowest birth rate in LOW-COUNTY County, WA: x.xxx per 541
Highest birth rate in HIGH-COUNTY County, WA: x.xxx per 541
REPORT
**總是**用'use strict;啓動一個Perl程序;使用警告;'當你打開一個文件時,測試一下你是否成功了perl [perldoc中的示例](http://perldoc.perl.org/functions/open.html)。 – Quentin
並且不要推出自己的CSV解析器,使用[Text :: CSV](https://metacpan.org/module/Text::CSV),它更快,並且處理大量的邊緣情況。 – Quentin
@Quentin:原則上我同意'Text :: CSV'的建議,但看起來OP在使用模塊之前還有很長的路要走。這是學習曲線的一部分 – Zaid