- 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
推薦閱讀
- Java程序設計實戰教程
- 深入淺出Electron:原理、工程與實踐
- Java Web開發之道
- Java程序設計與計算思維
- Mastering C# Concurrency
- 單片機應用與調試項目教程(C語言版)
- Oracle GoldenGate 12c Implementer's Guide
- SciPy Recipes
- Visual Basic程序設計(第三版)
- Java程序設計教程
- Get Your Hands Dirty on Clean Architecture
- 第五空間戰略:大國間的網絡博弈
- Python機器學習
- Improving your Penetration Testing Skills
- Visual FoxPro數據庫程序設計