官术网_书友最值得收藏!

  • Perl 6 Deep Dive
  • Andrew Shitov
  • 175字
  • 2021-07-03 00:05:48

Methods of the Hash class

Let's see what methods are available for the hashes.

First, the two methods, keys and values, return lists (sequences, to be strict) containing all the keys and values of the hash.

my %capitals = 
Spain => 'Madrid',
Italy => 'Rome',
France => 'Paris';

my @countries = %capitals.keys;
my @cities = %capitals.values;

say @countries; # [France Italy Spain]
say @cities; # [Paris Rome Madrid]

The kv method returns a list of both keys and values:

say %capitals.kv; # (France Paris Italy
# Rome Spain Madrid)

A similar method, pairs, returns a list of pairs (pairs are data types containing a key and a value):

say %capitals.pairs; # (France => Paris 
# Italy => Rome
# Spain => Madrid)

To invert the pairs, use the antipairs method, as shown here:

say %capitals.antipairs; # (Paris => France 
# Rome => Italy
# Madrid => Spain)

The size of the hash, which is actually the number of pairs in it, is returned by the elems method, shown as follows:

say %capitals.elems; # 3
主站蜘蛛池模板: 常宁市| 乳山市| 类乌齐县| 和静县| 呼玛县| 乌什县| 甘南县| 丰台区| 美姑县| 轮台县| 北流市| 孙吴县| 白沙| 皋兰县| 五指山市| 金沙县| 永和县| 沛县| 元朗区| 额济纳旗| 保亭| 德令哈市| 获嘉县| 云南省| 壶关县| 涪陵区| 合川市| 清徐县| 获嘉县| 彝良县| 平湖市| 玉环县| 大埔县| 瓦房店市| 于都县| 曲靖市| 托克托县| 新宁县| 新闻| 彭阳县| 芮城县|