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

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
主站蜘蛛池模板: 南投县| 石嘴山市| 汨罗市| 邯郸市| 宿州市| 桃园县| 龙里县| 红安县| 兰溪市| 阿勒泰市| 长子县| 神木县| 宝山区| 昌乐县| 阜城县| 宁南县| 应城市| 铁力市| 中江县| 东乡| 乌海市| 清丰县| 廊坊市| 湛江市| 洛川县| 扬中市| 安仁县| 洮南市| 察雅县| 收藏| 昌平区| 启东市| 平原县| 神农架林区| 子洲县| 陆川县| 湖南省| 涡阳县| 江阴市| 福清市| 安达市|