- 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
推薦閱讀
- 數(shù)據(jù)科學(xué)實戰(zhàn)手冊(R+Python)
- 從零開始:數(shù)字圖像處理的編程基礎(chǔ)與應(yīng)用
- C語言程序設(shè)計案例教程(第2版)
- ASP.NET Core 5.0開發(fā)入門與實戰(zhàn)
- Visual C++串口通信技術(shù)詳解(第2版)
- aelf區(qū)塊鏈應(yīng)用架構(gòu)指南
- Linux網(wǎng)絡(luò)程序設(shè)計:基于龍芯平臺
- Ray分布式機器學(xué)習(xí):利用Ray進行大模型的數(shù)據(jù)處理、訓(xùn)練、推理和部署
- Scratch 3.0少兒編程與邏輯思維訓(xùn)練
- JS全書:JavaScript Web前端開發(fā)指南
- Interactive Applications Using Matplotlib
- HTML5秘籍(第2版)
- Service Mesh實戰(zhàn):基于Linkerd和Kubernetes的微服務(wù)實踐
- Qt5 C++ GUI Programming Cookbook
- Java并發(fā)編程:核心方法與框架