- 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.
- Instant Apache Stanbol
- LabVIEW入門與實戰開發100例
- .NET 4.0面向對象編程漫談:基礎篇
- Python程序設計(第3版)
- Mastering LibGDX Game Development
- 微信小程序開發解析
- 移動界面(Web/App)Photoshop UI設計十全大補
- Android系統級深入開發
- Building Android UIs with Custom Views
- 微服務架構深度解析:原理、實踐與進階
- Java零基礎實戰
- SciPy Recipes
- HTML+CSS+JavaScript網頁制作:從入門到精通(第4版)
- Building Slack Bots
- Python計算機視覺與深度學習實戰