Perl中的哈希和智能匹配运算符 [英] Hashes and Smart Match Operators in Perl

查看:541
本文介绍了Perl中的哈希和智能匹配运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须为作业写一个程序。我被告知使用智能匹配运算符或哈希。我的问题是,我不明白他们是如何工作的,或者他们做了什么,无论我在教科书或互联网上阅读什么。我不认为有人可以简单地向我解释他们的功能吗?



作业的最后一部分是推导一个地区独特种类的数量 - 已经计算出哪些物种在该区域,但我不知道如何计算物种总数。例如,如果在一个很小的区域有4只鸟:

  2红色风筝
1 Robin
1 Duck

我知道总共有4只鸟,但我不知道如何确定该特定区域有3种。

解决方案

嗯,智能匹配是实验性的,可能会改变,所以我不会使用它。 (很明显,smartmatch几乎肯定会在未来发生变化或消失,不推荐依赖其当前行为。

散列是散列表的实现,这是一种用于快速查找元素的数据结构键入一个字符串。



散列的另一个特点是可以枚举元素内的键(使用 keys 在列表上下文中),你可以对它们进行计数(在标量上下文中使用 keys )。这对分组项目很有用。在执行以下操作后,请考虑存在哪些元素:

  $ birds {Red Kite} = 1; 
$ birds {Red Kite} = 1;
$ birds {Robin} = 1;
$ birds {Duck} = 1;

您最终得到三个元素,它们的键是 Red Kite Robin Duck 。只需使用循环代替对值进行硬编码来填充散列,然后使用来计算结果元素的数量,然后获得答案。






奖金:我在上面使用了一个任意值,因为它足以实现您的目标。但考虑如果您增加了元素的值而不是设置它,您会得到什么。

  ++ $ birds {Red风筝}; 
++ $ birds {Red Kite};
++ $ birds {Robin};
++ $ birds {Duck};

你得到每个物种的鸟数!


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.

解决方案

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!

这篇关于Perl中的哈希和智能匹配运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆