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

  • 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.

主站蜘蛛池模板: 炉霍县| 封开县| 丰台区| 浦城县| 巴塘县| 武陟县| 北安市| 广河县| 祁东县| 鸡东县| 得荣县| 昭苏县| 漯河市| 托克逊县| 监利县| 泊头市| 十堰市| 石阡县| 刚察县| 台中市| 黑山县| 开封县| 宁波市| 涪陵区| 当雄县| 抚宁县| 明星| 五大连池市| 大城县| 阿拉善左旗| 沙洋县| 安远县| 宜丰县| 天镇县| 屏东市| 青海省| 独山县| 信丰县| 嵊泗县| 棋牌| 清流县|