Skip to content

Perl Arrays Guide — Ordered Lists of Scalars and Array Operations

DodaTech Updated 2026-06-28 3 min read

In this tutorial, you will learn about Perl Arrays Guide. We cover key concepts, practical examples, and best practices to help you master this topic.

Perl arrays (@array) are ordered collections of scalars indexed by integers starting at 0, with built-in functions for adding, removing, and transforming elements in both stack and queue patterns.

Creating Arrays

use strict;
use warnings;

# Simple array
my @colors = ("red", "green", "blue");

# Using qw (quote word) -- no commas or quotes
my @numbers = qw(10 20 30 40 50);

# Range operator
my @range = (1..10);
my @letters = ('a'..'z');

# Interpolation
my @names = ("Alice", "Bob", "Charlie");

Accessing Elements

my @items = qw(apple banana cherry date);

# Index access (0-based)
print $items[0];  # apple
print $items[1];  # banana
print $items[-1]; # date (negative index from end)

# Slice -- multiple elements
my @first_two = @items[0, 1];  # (apple, banana)
my @subset = @items[1..3];     # (banana, cherry, date)

# Last index
print $#items;  # 3 (last index, not length)

Array Operations

my @stack = ();

# Stack operations (LIFO)
push @stack, "first";   # add to end
push @stack, "second";  # add to end
my $last = pop @stack;  # remove from end
print $last;  # second

# Queue operations (FIFO)
my @queue = ();
push @queue, "item1";   # enqueue
push @queue, "item2";
my $first = shift @queue;  # dequeue
print $first;  # item1

# Unshift (add to beginning)
unshift @queue, "new_item";

Array Functions

my @arr = (3, 1, 4, 1, 5, 9);

# Size (scalar context)
my $size = @arr;
print $size;  # 6

# Sort (returns new array)
my @sorted = sort @arr;  # (1, 1, 3, 4, 5, 9)

# Reverse
my @reversed = reverse @arr;  # (9, 5, 1, 4, 1, 3)

# Join into string
my $str = join(", ", @arr);  # "3, 1, 4, 1, 5, 9"

# Split string into array
my @parts = split(", ", $str);  # back to array

Context and Arrays

my @items = (10, 20, 30);

# Scalar context: returns count
my $count = @items;
print $count;  # 3

# List context: returns elements
my @copy = @items;

# Forced scalar context
my $last_index = $#items;  # 2

Common Mistakes

1. Using $ instead of @ for array access

Individual elements use $: $arr[0]. The whole array uses @: @arr.

2. Forgetting arrays are 0-indexed

First element is $arr[0], not $arr[1].

3. Confusing push and unshift

push adds to end, unshift adds to beginning. pop removes from end, shift removes from beginning.

Practice Questions

1. How do you get the length of an array? Use my $len = @array; (scalar context) or my $last_idx = $#array; and add 1.

2. What do push and pop do? push adds to the end, pop removes from the end. They treat the array as a stack (LIFO).

3. How do you extract a slice of an array? Use @array[2, 4] or @array[1..3] to get multiple elements at once.

FAQ

{{< faq question="What is the difference between @array and @array[0]?" >}} @array is the entire array (list context). @array[0] is a slice returning a list with one element (still a list). $array[0] is a single scalar element. {{< /faq >}}

{{< faq question="How do I remove duplicate elements?" >}} Use my %seen; my @unique = grep { !$seen{$_}++ } @array; or the List::MoreUtils module's uniq function. {{< /faq >}}

{{< faq question="Can arrays contain different types?" >}} Yes. Perl arrays can hold scalars of any type -- numbers, strings, undef, and references to other data structures. {{< /faq >}}

What's Next

Now learn about Perl hashes -- key-value data structures.

Topic Description Link
Hashes Key-value pairs {{< ref "05-hashes" >}}
Regex Regular expressions {{< ref "06-regex" >}}

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro