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

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
主站蜘蛛池模板: 南漳县| 广宁县| 明水县| 马尔康县| 和田市| 鞍山市| 新宁县| 平山县| 牙克石市| 喀喇沁旗| 湘西| 威信县| 大方县| 剑河县| 江永县| 岱山县| 望奎县| 琼结县| 安多县| 资兴市| 卫辉市| 崇仁县| 邻水| 云浮市| 元氏县| 南郑县| 宝兴县| 安义县| 白河县| 六安市| 镇巴县| 彩票| 大悟县| 山阳县| 耿马| 松阳县| 乌拉特中旗| 富蕴县| 井冈山市| 桑植县| 新蔡县|