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

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
主站蜘蛛池模板: 甘孜| 巴林左旗| 通江县| 农安县| 清丰县| 新泰市| 常宁市| 巨野县| 永定县| 嘉峪关市| 克拉玛依市| 扶绥县| 安国市| 陆河县| 广西| 宝鸡市| 马山县| 元氏县| 易门县| 湖州市| 黄大仙区| 涟源市| 麻栗坡县| 岳阳县| 木兰县| 驻马店市| 木里| 孝感市| 中西区| 抚松县| 巨野县| 永城市| 兴宁市| 榆中县| 云浮市| 玛多县| 阿拉善右旗| 长葛市| 呼和浩特市| 福泉市| 酒泉市|