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

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.

主站蜘蛛池模板: 石嘴山市| 苍山县| 宁都县| 山阳县| 阿坝| 苏尼特左旗| 将乐县| 山西省| 乌鲁木齐市| 沅陵县| 青冈县| 荥经县| 保德县| 青州市| 林芝县| 肥西县| 色达县| 综艺| 永安市| 建瓯市| 广德县| 保康县| 凌云县| 嘉峪关市| 敖汉旗| 吴江市| 砀山县| 孙吴县| 太白县| 灵武市| 册亨县| 乌兰县| 雅江县| 英超| 建昌县| 达拉特旗| 瑞金市| 昌邑市| 盐亭县| 涞源县| 繁峙县|