官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 岱山县| 灵武市| 格尔木市| 台中县| 鄂伦春自治旗| 陕西省| 新宁县| 河南省| 双辽市| 太白县| 和静县| 百色市| 望江县| 西华县| 黄梅县| 台安县| 康乐县| 鹰潭市| 甘南县| 岑巩县| 海城市| 奎屯市| 雷波县| 呼伦贝尔市| 台北市| 婺源县| 柯坪县| 蛟河市| 隆安县| 锦州市| 汶上县| 石渠县| 阳城县| 雷州市| 正镶白旗| 剑阁县| 永德县| 扶风县| 庆城县| 永昌县| 镶黄旗|