- Perl 6 Deep Dive
- Andrew Shitov
- 230字
- 2021-07-03 00:05:48
Arrays
Array variables can host more than one value. The values can be of the same type, or can be of different types. Arrays are often used to keep lists of data items.
Arrays in Perl 6 are prefixed with the @ sigil. To access the elements of an array, the postfix pair of square brackets is used. For example, the second element of the @a array is @a[1]. Note that indexing starts from zero.
Let's take a look at how to create an array of integer numbers:
my @odd_numbers = 1, 3, 5, 7, 9, 11;
Alternatively, you can use parentheses or angle brackets. The following two arrays are the same as the previous one:
my @array2 = (1, 3, 5, 7, 9, 11);
my @array3 = <1 3 5 7 9 11>;
When printing it using the say built-in function, Perl 6 prints the content of the array in square brackets, as you can see here:
say @odd_numbers; [1 3 5 7 9 11]
Here is another example of an array that contains data of mixed types:
my @array = 1, 'two', 3E-2;
All the elements here are of different types (integer, string, and floating-point value), but they can easily be accessed via their index:
say @array[0]; # 1
say @array[1]; # two
say @array[2]; # 0.03
Let's take a further look at the possibilities that arrays offer in Perl 6.
- GAE編程指南
- 流量的秘密:Google Analytics網站分析與優化技巧(第2版)
- DevOps with Kubernetes
- Objective-C應用開發全程實錄
- Hands-On Data Structures and Algorithms with JavaScript
- Python程序設計
- Elasticsearch Server(Third Edition)
- 深入理解Android:Wi-Fi、NFC和GPS卷
- PHP編程基礎與實踐教程
- GitHub入門與實踐
- jQuery技術內幕:深入解析jQuery架構設計與實現原理
- 程序員必會的40種算法
- 量子計算機編程:從入門到實踐
- Web前端測試與集成:Jasmine/Selenium/Protractor/Jenkins的最佳實踐
- Java編程指南:語法基礎、面向對象、函數式編程與項目實戰