Perl Variables  «Prev  Next»
Lesson 7Perl Arrays
ObjectiveExamine an array in Perl

Perl Arrays and named list

In Perl 6, which has been officially renamed to Raku, arrays are ordered collections of elements indexed by contiguous integer keys starting from zero. The syntax for declaring and manipulating arrays has been modernized and enhanced to offer more readability, flexibility, and power compared to its Perl 5 predecessor.

Perl Syntax Overview

In Raku, arrays are typically denoted with the `@` sigil, and the elements are enclosed within square brackets `[]`. Individual elements within the array are separated by commas. Element access is done through the square bracket notation with the index starting from 0 for the first element.

Declaration and Initialization

  1. Declaring an Empty Array:
    my @empty-array;
    
  2. Initializing with Values:
  3. my @numbers = 1, 2, 3, 4, 5;
    
    or
    my @numbers = [1, 2, 3, 4, 5];
    

Element Access and Assignment

  1. Access:
    To access an individual element, the array variable is followed by the index enclosed within square brackets.
    my $first-element = @numbers[0];  # retrieves the value '1'
    
  2. Assignment:
    Elements can be modified by assigning a new value to the specific index.
    @numbers[0] = 10;  # replaces the first element with '10'
    

Perl Slicing

Arrays can be sliced to produce new arrays consisting of a subset of the elements.
my @subset = @numbers[1..3];  # retrieves a slice containing elements at indices 1, 2, and 3

Built-in Array Operations

Raku offers a rich set of array operations including but not limited to:
  1. `push`: Add elements to the end of the array
  2. `pop`: Remove and return the last element
  3. `shift`: Remove and return the first element
  4. `unshift`: Add elements to the beginning
@numbers.push(6);       # [10, 2, 3, 4, 5, 6]
@numbers.pop;           # removes '6', returns array [10, 2, 3, 4, 5]
@numbers.shift;         # removes '10', returns array [2, 3, 4, 5]
@numbers.unshift(1);    # [1, 2, 3, 4, 5]

Typed Arrays

Raku allows for typed arrays, where each element must conform to a particular type.
my Int @integer-array = 1, 2, 3;
In summary, Raku arrays provide a powerful and flexible mechanism for ordered data collection, with a streamlined syntax and a host of built-in operations that make array manipulation both convenient and efficient.
Just as a scalar variable is a scalar value with a name assigned to it, an array is a named list.
In fact, an array is simply another name for a list variable. To initialize an array, simply assign a list to it like this:

@characters = ("dave", "frank", "hal");

Notice that an array-type variable is indicated with the @ character, much like a scalar is indicated with $.


Diagram of a array
Diagram of a array
If you wanted to list each of the names in the @characters array, you could use a foreach loop:
foreach $c (@characters) { 
  print "$c\n" 
}

Or, you could achieve the same net effect with the join function:
print join ("\n", @characters);

With an array, you can store lists of related items and then access them in an orderly fashion.
In the next lesson, we will look at some of the different ways you can access arrays.