问题
For a programme that I must write for an assignment. I've been told to use either a Smart Match Operator or Hashes. My problem is, is that I just don't understand how they work or what they do no matter what I read in textbooks or on the internet. I don't suppose somebody could explain their function to me really simply?
The final part of the assignment is to deduce the number of unique species in an area - I've calculated which species are the in the area but I don't know how to calculate the total number of species.
For example if in a tiny area there are 4 birds:
2 Red Kite
1 Robin
1 Duck
I know there are 4 birds in total but I don't know how to work out that there are 3 species in that particular area.
回答1:
I suggest you use a hash, which is a data structure that you can access using a string.
You can compare hashes to arrays.
Each element of an array has an index and a value, so the value of the element of @data
for index 2
is $data[2]
. Similarly, each element of a hash has a key and a value, so the value of the element of %bird_count
for key Red Kite
is $bird_count{'Red Kite'}
. (Notice the use of braces instead of brackets to access hash elements.)
Where an array like @data
can be accessed using integers, so the third element is $data[2]
, a hash like %bird_counts
can be accessed using a string, so the count for Red Kites is $bird_count{'Red Kite'}
.
If you had a program that counted birds as they were observed, it could do
++$bird_count{'Red Kite'};
++$bird_count{'Robin'};
++$bird_count{'Red Kite'};
++$bird_count{'Duck'};
after which the hash would have three elements: $bird_count{'Red Kite'}
would be 2
and the other two would be 1
. Try it.
You can count the number of different birds by counting the number of keys of the hash, so
my $num_different_birds = keys %bird_count;
I hope that helps.
回答2:
Well, smart match is experimental and likely to be changed, so I wouldn't use it. (It is clear that smartmatch is almost certainly either going to change or go away in the future. Relying on its current behavior is not recommended.)
A hash is an implementation of a hash table, a data structure designed for quick lookup of elements keyed by a string. Think of it as an array with strings for indexes rather than integers.
Another feature of hashes is that you can can enumerate the keys of the elements within (using keys
in list context) and you can count them (using keys
in scalar context). This can be useful for grouping items. Consider what elements exist after you execute the following:
$birds{"Red Kite"} = 1;
$birds{"Red Kite"} = 1;
$birds{"Robin"} = 1;
$birds{"Duck"} = 1;
You end up with three elements whose keys are Red Kite
, Robin
and Duck
. Just use a loop instead of hardcoding the values to populate the hash, then use keys
to count the number of resulting elements, and you got your answer.
Bonus: I just used an arbitrary value in the above, since it was sufficient to achieve your goal. But consider what you would get if you incremented the value of the element instead of setting it.
++$birds{"Red Kite"};
++$birds{"Red Kite"};
++$birds{"Robin"};
++$birds{"Duck"};
You end up with the number of birds of each species!
来源:https://stackoverflow.com/questions/22206061/hashes-and-smart-match-operators-in-perl