我刚刚完成了一项任务 - 我上周问了一个关于这个问题的问题,并被建议不要使用智能匹配操作员,但我对Hashes有更深入的了解。
这个子程序基本上是4个子程序之一。它打开一个包含125,000个哺乳动物的文件,将其加载到列表中,循环遍历列表并将其拆分为标量。将拆分列表的一部分加载到计算器中,该计算器返回一个值($ Calcdist)。
我想要它做什么基本上计算该区域的物种数量。有些。 我目前使用的数据是:
$File =("Mammal.txt");
$Distance = 5.0;
$Lat = 51.75;
$Long = -1.25;
根据这些信息,它告诉我该区域有95只动物(使用另一个子程序)这个子程序应该告诉我有30种独特的物种,但它打印的是9号。
我哪里出错了?是我的'如果'和'其他'不正确的?我知道有更好的编码perl的方法,但这只是我教过的方式。
sub BiodiversityCount ($$$$)
{
my ($Filename, $Distance, $Lat1, $Lon1) = @_;
my ($Line, $Index, $CalcDist, $Counter, $Counter2, $Index2);
my (@List, @Part, @Species);
open (INF, "<", $Filename) or die ("Cannot open Mammal.txt\n");
@List = <INF>;
close (INF);
for ($Index=0;$Index<@List;$Index++)
{
@Part = split ("\t", $List[$Index]);
$CalcDist = CalculateDistance ($Lat1, $Lon1, $Part[1], $Part[2]);
if ($CalcDist<$Distance)
{
if ($Part[0] ~~ @Species)
{
$Counter++
}
else
{
@Species = $Part[0];
}
}
}
#printf @Species;
print("The number of unique species found upto $Distance km from $Lat1, $Lon1 is: $Counter\n");
#Number of Unique species found in an area
#Smart match operator
#Hashes - declared using my %___
}
BiodiversityCount ($File, $Distance, $Lat, $Long);
测试数据:
Muntiacus reevesi
Muntiacus reevesi
Apodemus flavicollis
Mustela erminea
Lepus europaeus
Apodemus sylvaticus
Sciurus carolinensis
Talpa europaea
Arvicola amphibius
Rhinolophus ferrumequinum
Arvicola amphibius
Muntiacus reevesi
Apodemus sylvaticus
Sciurus carolinensis
Sciurus carolinensis
Sorex araneus
Erinaceus europaeus
Sciurus vulgaris
Vulpes vulpes
Clethrionomys glareolus
Sorex araneus
Meles meles
Arvicola amphibius
Microtus agrestis
Vulpes vulpes
Nyctalus noctula
Erinaceus europaeus
Neomys fodiens
Meles meles
Erinaceus europaeus
Capreolus capreolus
Pipistrellus pipistrellus sensu lato
Micromys minutus
Rattus norvegicus
Sorex minutus
Plecotus
Erinaceus europaeus
Muntiacus reevesi
Vulpes vulpes
Mustela erminea
Oryctolagus cuniculus
Dama dama
Erinaceus europaeus
Talpa europaea
Muntiacus reevesi
Muntiacus reevesi
Sciurus carolinensis
Vulpes vulpes
Sorex minutus
Talpa europaea
Talpa europaea
Muntiacus reevesi
Muntiacus reevesi
Muntiacus reevesi
Mustela vison
lethrionomys glareolus
Microtus agrestis
Sciurus vulgaris
Mustela nivalis
Apodemus sylvaticus
Meles meles
Meles meles
Vulpes vulpes
Capreolus capreolus
Mustela nivalis
Mustela putorius
Microtus agrestis
Sorex araneus
Sorex minutus
Muscardinus avellanarius
Muntiacus reevesi
Sciurus carolinensis
Dama dama
Muntiacus reevesi
Talpa europaea
Muntiacus reevesi
Microtus agrestis
Dama dama
Microtus agrestis
Talpa europaea
Arvicola amphibius
Muntiacus reevesi
Muntiacus reevesi
Muntiacus reevesi
Sorex minutus
Vulpes vulpes
Dama dama
Talpa europaea
Apodemus sylvaticus
Sorex araneus
Microtus agrestis
Lepus europaeus
Sorex minutus
Neomys fodiens
Vulpes vulpes
答案 0 :(得分:1)
这一行:
@Species = $Part[0];
应该是:
push @Species, $Part[0];
此外,您可能希望chomp
从文件中读取的行以删除尾随换行符。
您还至少有一个逻辑错误:我认为您应该只在++
not $Part[0] ~~ @Species
时才{{1}}。
答案 1 :(得分:1)
您可能需要哈希来计算独特的物种。我对使用散列%species_count
的代码进行了调整。您可能会进行其他更改,但就目前而言,我认为此哈希将为您提供所需的更改。 (我还捏造$ CalcDist = 1; #CalculateDistance($ Lat1,$ Lon1,$ Part [1],$ Part [2]);运行脚本,只需将其更改回原来的版本)
#!/usr/bin/perl
use strict;
use warnings;
my $File =("Mammal.txt");
my $Distance = 5.0;
my $Lat = 51.75;
my $Long = -1.25;
sub BiodiversityCount ($$$$)
{
my ($Filename, $Distance, $Lat1, $Lon1) = @_;
my ($Line, $Index, $CalcDist, $Counter, $Counter2, $Index2);
my (@List, @Part, %species_count);
#open (INF, "<", $Filename) or die ("Cannot open Mammal.txt\n");
chomp(@List = <DATA>);
#close (INF);
for ($Index=0;$Index<@List;$Index++)
{
@Part = split ("\t", $List[$Index]);
$CalcDist = 1;#CalculateDistance ($Lat1, $Lon1, $Part[1], $Part[2]);
if ($CalcDist<$Distance)
{
$species_count{$Part[0]}++;
}
}
#printf @Species;
#print("The number of unique species found upto $Distance km from $Lat1, $Lon1 is: $Counter\n");
print("The number of unique species found upto $Distance km from $Lat1, $Lon1 is: ", scalar keys %species_count, "\n");
#Number of Unique species found in an area
#Smart match operator
#Hashes - declared using my %___
}
BiodiversityCount ($File, $Distance, $Lat, $Long);
__DATA__
Muntiacus reevesi
Muntiacus reevesi
Apodemus flavicollis
Mustela erminea
Lepus europaeus
Apodemus sylvaticus
Sciurus carolinensis
Talpa europaea
Arvicola amphibius
Rhinolophus ferrumequinum
Arvicola amphibius
Muntiacus reevesi
Apodemus sylvaticus
Sciurus carolinensis
Sciurus carolinensis
Sorex araneus
Erinaceus europaeus
Sciurus vulgaris
Vulpes vulpes
Clethrionomys glareolus
Sorex araneus
Meles meles
Arvicola amphibius
Microtus agrestis
Vulpes vulpes
Nyctalus noctula
Erinaceus europaeus
Neomys fodiens
Meles meles
Erinaceus europaeus
Capreolus capreolus
Pipistrellus pipistrellus sensu lato
Micromys minutus
Rattus norvegicus
Sorex minutus
Plecotus
Erinaceus europaeus
Muntiacus reevesi
Vulpes vulpes
Mustela erminea
Oryctolagus cuniculus
Dama dama
Erinaceus europaeus
Talpa europaea
Muntiacus reevesi
Muntiacus reevesi
Sciurus carolinensis
Vulpes vulpes
Sorex minutus
Talpa europaea
Talpa europaea
Muntiacus reevesi
Muntiacus reevesi
Muntiacus reevesi
Mustela vison
lethrionomys glareolus
Microtus agrestis
Sciurus vulgaris
Mustela nivalis
Apodemus sylvaticus
Meles meles
Meles meles
Vulpes vulpes
Capreolus capreolus
Mustela nivalis
Mustela putorius
Microtus agrestis
Sorex araneus
Sorex minutus
Muscardinus avellanarius
Muntiacus reevesi
Sciurus carolinensis
Dama dama
Muntiacus reevesi
Talpa europaea
Muntiacus reevesi
Microtus agrestis
Dama dama
Microtus agrestis
Talpa europaea
Arvicola amphibius
Muntiacus reevesi
Muntiacus reevesi
Muntiacus reevesi
Sorex minutus
Vulpes vulpes
Dama dama
Talpa europaea
Apodemus sylvaticus
Sorex araneus
Microtus agrestis
Lepus europaeus
Sorex minutus
Neomys fodiens
Vulpes vulpes
答案 2 :(得分:0)
您似乎尝试使用~~
运算符(错误地)执行集合成员资格测试。
Smartmatch已被重新宣布为实验性的,因此您可能希望避免使用它。您可以避免它,同时通过重写代码使您的代码在意图中更清晰,并且实际上可以工作。例如,您可以使用any
中的List::Util
函数:
if ($Part[0] ~~ @Species)
现在变成
if ( any { $Part[0] eq $_ } @Species )
一般来说,你应该暂时避免使用smartmatch,除非并且直到核心perl为它确定了更好的语义集,并且再次声明它是非实验性的。