- 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
推薦閱讀
- MySQL數(shù)據(jù)庫(kù)管理實(shí)戰(zhàn)
- Python程序設(shè)計(jì)教程(第2版)
- JavaScript全程指南
- C# Programming Cookbook
- Practical Data Science Cookbook(Second Edition)
- PhoneGap Mobile Application Development Cookbook
- Microsoft System Center Orchestrator 2012 R2 Essentials
- 區(qū)塊鏈國(guó)產(chǎn)化實(shí)踐指南:基于Fabric 2.0
- QPanda量子計(jì)算編程
- Flask Web開(kāi)發(fā):基于Python的Web應(yīng)用開(kāi)發(fā)實(shí)戰(zhàn)(第2版)
- 大數(shù)據(jù)時(shí)代的企業(yè)升級(jí)之道(全3冊(cè))
- Python網(wǎng)絡(luò)爬蟲(chóng)實(shí)例教程(視頻講解版)
- 安卓工程師教你玩轉(zhuǎn)Android
- LiveCode Mobile Development Hotshot
- 亮劍Java Web項(xiàng)目開(kāi)發(fā)案例導(dǎo)航