Arrays & Lists

    In Perl 6, the qw is simplified:

    1. my @stooges = < Larry Curly Moe Iggy >;
    2. # or
    3. my @stooges = <
    4. Larry
    5. Curly
    6. Moe
    7. Iggy
    8. >;

    The elements do not interpolate, so:

    1. my @array = qw( $100,000 ); # Perl 5
    2. my @array = < $100,000 >; # Perl 6

    The single element of @array is '$100,000'.

    Access arrays with numeric values

    To get a single scalar out of an array, access it with the [] and a $ sigil. All arrays in Perl start at 0.

    1. $stooges[1] # Curly

    Arrays can also be accessed from the end by using negative offsets from the end.

    1. $stooges[-1] # Iggy

    Reading a non-existent element gives undef.

    1. $stooges[47] # undef

    The length of an array is its scalar value

    Put an array in scalar context to get its length. Some people like to explicitly use scalar. Some don't.

    1. my $stooge_count = scalar @stooges; # 4
    2.  
    3. my $stooge_count = @stooges; # 4

    Don't take the length of an array with length. That gives the length of a string.

    1. my $moe_length = length $stooges[@stooges/2];
    2. length $stooges[2];
    3. length 'Moe';

    Arrays have no boundaries

    Arrays don't have any finite size, and don't have to be predeclared. Arrays change in size as necessary.

    Arrays are also not sparse. This code makes a 10,000-element array.

    1. my @array = ();
    2. $array[10000] = 'x';

    @array is now 10,001 elements long (0-10,000), of which only one is populated. The other 10,000 are undef.

    This means you can't have an array "contain" another array, or a hash. To do that, you need references .

    Lists can have extra commas

    One of the greatest features of Perl is the ability to have an extra comma at the end of a list. For example:

    1. my @array = (
    2. 'This thing',
    3. 'That thing',
    4. );

    This makes adding or deleting items super easy when you edit your code, since you don't have to treat the last as a special case:

    1. my @array = (
    2. 'This thing',
    3. 'That thing',
    4. 'The other thing',
    5. );

    Use arrays like queues and stacks

    removes from the beginning of the array:

    1. my $next_customer = shift @customers;

    unshift adds to the beginning of an array:

    1. unshift @customers, $line_jumper;

    push adds to the end of an array:

    1. push @customers, $dio; # The last in line

    pop removes from the end of an array:

    1. my $went_home = pop @customers;

    Extract sections of an array with array slices

    Array slices are just array accesses with multiple indices.

    1. my @a = 'a'..'z'; # 26 letters
    2.  
    3. # a, e, i, o, u...
    4. my @vowels = @a[0,4,8,14,20];
    5.  
    6. # And sometimes "y"
    7. push( @vowels, $a[-2] ) if rand > .5;

    Note that when accessing an array slice, the sigil is @, not $, because you're returning an array, not a scalar. A common mistake for beginners is to access an array element with a @ sigil, not $, and get back a slice, which is a list:

    1. # WRONG: Returns a 1-element list, or 1 in scalar context
    2. my $z = @a[-1];
    3.  
    4. # RIGHT: Returns a single scalar element
    5. my $z = $a[-1];

    You can have array slices as _lvalue_s, or values on the left side of the equals sign that can be assigned to.

    Note that the left and right slices must be of the same size. Any missing values on the right side of the equals sign will be replaced with undef.

    Modify arrays in-place with splice

    1. my @a = qw(Steve Stu Stan);
    2. $a[1] = ['Stewart', 'Zane'];
    3. # @a = ('Steve', ARRAY(0x841214c), 'Stan')
    4. # Memory address to an array reference
    5.  
    6. my @b = qw(Stewart Zane);
    7. $a[1] = @b;
    8. # @a = ('Steve', 2, 'Stan')
    9. # Returns a scalar reference, the length of @b

    Now let's use splice:

    1. @a = qw(Steve Stu Stan);
    2. splice @a, 1, 1, 'Stewart', 'Zane';
    3. # @a = ('Steve', 'Stewart', 'Zane', 'Stan')
    4. # This is just what we wanted

    Let's break down the argument list for : first, we name the array we're operating on (@a); second, we define the offset (how far into the list we want the splice to start); third, we specify the length of the splice (how far forward from the offset we'll be deleting to make room for the new list); finally, we list the items we want inserted.

    If all else fails, perldoc -f splice.

    Process arrays easily with map

    map is essentially a foreach loop that returns a list.

    You can use it to convert an array into a hash:

    1. my @array = ( 1, 2, 3, 4, 5 );
    2. my %hash = map { $_ => $_ * 9 } @array;
    3. # %hash = ( 1 => 9, 2 => 18, 3 => 27, 4 => 36, 5 => 45 )

    or to transform a list:

    1. my @array = ( 'ReD', 'bLue', 'GrEEN' );
    2. my @fixed_array = map(ucfirst, map(lc, @array)); # note the nested 'map' functions
    3. # @fixed_array = ( 'Red', 'Blue', 'Green' )

    Watch out, though: If you modify $_, you will modify the source data. This means the above might be changed to:

    1. my @array = ( 'ReD', 'bLue', 'GrEEN' );
    2. map { $_ = ucfirst lc $_ } @array;
    3. # @array = ( 'Red', 'Blue', 'Green' )

    Select items out of an array with grep

    grep is essentially a foreach loop that returns a list, but unlike map, it will only return elements that cause the condition to return true.

    1. my @array = ( 0, 1, 2, 3, 4, 5 );
    2. my @new_array = grep { $_ * 9 } @array;
    3. # @new_array = ( 1, 2, 3, 4, 5 );

    It will modify the source data the same way as map, however:

    1. my @array = ( 0, 1, 2, 3, 4, 5 );
    2. my @new_array = grep { $_ *= 9 } @array;
    3. # @array = ( 0, 9, 18, 27, 36, 45 );
    4. # @new_array = ( 9, 18, 27, 36, 45 );

    We can also pass regular expressions into grep; in this example, we want only people with the surname 'Doe' put into our new array:

    1. my @people = (
    2. 'John Doe',
    3. 'Jane Doe',
    4. 'Joe Sixpack',
    5. 'John Q. Public',
    6. );
    7.  
    8. my @does = grep { $_ =~ /\bDoe$/ } @people;

    or even shorter