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

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
主站蜘蛛池模板: 廊坊市| 台东县| 巴青县| 扶绥县| 孟津县| 梁平县| 东乌珠穆沁旗| 黄冈市| 比如县| 桃园县| 泊头市| 定州市| 克什克腾旗| 常德市| 封丘县| 余姚市| 偏关县| 玛多县| 惠水县| 平顺县| 历史| 嘉义市| 博罗县| 铅山县| 离岛区| 富源县| 鹤岗市| 临夏市| 福鼎市| 道真| 开化县| 久治县| 铜山县| 陇西县| 石屏县| 高清| 浙江省| 甘洛县| 兴安盟| 竹山县| 蓬安县|