- Perl 6 Deep Dive
- Andrew Shitov
- 314字
- 2021-07-03 00:05:48
Hashes
In the array, indexes are integer numbers starting from zero. Hashes are another structural type of data in Perl 6 that can be treated as arrays whose indices are strings.
Hashes use the % sigil. Other names for hashes from various programming languages are associative arrays, dictionaries or dicts, and maps. Hashes are very useful when you need to keep a few values together. For example, take a look at the following code snippet:
my %city =
name => 'London',
country => 'gb',
latitude => 51.52,
longitude => 0,
area => 1577,
inhabitants => 8_700_000;
Elements of the hash are pairs, which are in turn two things—the key and the value. In this example, keys of the %city hash are name, country, and so on, and their values are London and gb.
The layout of the code in such assignments may be changed to align the keys and the values, as you can see here:
my %city =
name => 'London',
country => 'gb',
latitude => 51.52,
longitude => 0,
area => 1577,
inhabitants => 8_700_000;
In the assignment, pairs of the hash can be surrounded in parentheses, as shown here:
my %city = (
name => 'London',
country => 'gb',
latitude => 51.52,
longitude => 0,
area => 1577,
inhabitants => 8_700_000);
When a hash is printed (say %city), it is displayed in a pair of curly braces, as shown in the following lines of code:
{area => 1577, country => gb, inhabitants => 8700000,
latitude => 51.52, longitude => 0, name => London}
If there are keys with identical names, then the last one wins. Consider the following hash creation:
my %city =
name => 'London',
name => 'Paris';
say %city;
This program prints {name => Paris} only.
The information in this section is enough to continue on our way to learning types in Perl 6.
- Python程序設(shè)計教程(第2版)
- 編程卓越之道(卷3):軟件工程化
- Ext JS Data-driven Application Design
- OpenNI Cookbook
- Python高效開發(fā)實戰(zhàn):Django、Tornado、Flask、Twisted(第2版)
- Python漫游數(shù)學(xué)王國:高等數(shù)學(xué)、線性代數(shù)、數(shù)理統(tǒng)計及運籌學(xué)
- JavaScript入門經(jīng)典
- Scala程序員面試算法寶典
- 人工智能算法(卷1):基礎(chǔ)算法
- CodeIgniter Web Application Blueprints
- Puppet 5 Beginner's Guide(Third Edition)
- Elasticsearch搜索引擎構(gòu)建入門與實戰(zhàn)
- C# 10核心技術(shù)指南
- Learning Azure DocumentDB
- Java程序設(shè)計基礎(chǔ)教程