Perl Arrays & Lists
Arrays in Perl are ordered lists of scalars. They are prefixed with an @ symbol and can grow and shrink dynamically. Lists are ordered collections of scalars that are the data used to construct arrays.
Creating Arrays
# Simple array creation
@fruits = ("apple", "banana", "cherry");
@numbers = (1, 2, 3, 4, 5);
@mixed = ("hello", 42, 3.14, "world");
# Using qw (quote words) for cleaner syntax
@colors = qw(red green blue yellow);
@days = qw/Monday Tuesday Wednesday Thursday Friday/;
# Range operator
@digits = (0..9);
@letters = ('a'..'z');
Accessing Array Elements
# Accessing elements (0-based index)
print $fruits[0]; # "apple"
print $fruits[2]; # "cherry"
# Negative indices count from end
print $fruits[-1]; # "cherry" (last element)
print $fruits[-2]; # "banana" (second to last)
# Slices - accessing multiple elements
@first_two = @fruits[0,1]; # ("apple", "banana")
@every_other = @numbers[1,3]; # (2, 4)
Modifying Arrays
# Changing elements
$fruits[1] = "blueberry"; # Now ("apple", "blueberry", "cherry")
# Adding elements
push @fruits, "date"; # Add to end
unshift @fruits, "apricot"; # Add to beginning
# Removing elements
$last = pop @fruits; # Remove from end
$first = shift @fruits; # Remove from beginning
# Inserting elements
splice @numbers, 2, 0, (10, 11); # Insert 10,11 at position 2
# Deleting elements
splice @numbers, 1, 2; # Remove 2 elements starting at position 1
Array Operations
# Array length
$count = @fruits; # Scalar context gives length
$last_index = $#fruits; # Index of last element
# Joining elements
$joined = join ", ", @fruits; # "apple, banana, cherry"
# Splitting strings into arrays
$line = "root:x:0:0:root:/root:/bin/bash";
@fields = split /:/, $line;
# Sorting
@sorted = sort @fruits; # Alphabetical order
@reverse_sorted = reverse sort @fruits;
# Numeric sort
@numbers = (5, 2, 9, 1, 7);
@sorted_numbers = sort { $a <=> $b } @numbers;
Array Functions
# map - transform elements
@uppercased = map { uc } @fruits; # ("APPLE", "BANANA", "CHERRY")
# grep - filter elements
@long_words = grep { length($_) > 5 } @words;
# List assignment
($first, $second, @rest) = @array;
# Array replication
@double = (@numbers, @numbers);
Special Array Variables
# Command line arguments
@ARGV = ("file1.txt", "file2.txt");
# Environment variables
%ENV; # Hash containing environment variables
# Special arrays
@INC; # Array of paths to search for modules
@ISA; # Array of parent classes
Array Context
# Array in scalar context
$count = @array; # Number of elements
# Array in list context
@copy = @array; # Copies all elements
# Array slice
@first_three = @array[0..2];
# Array as stack
push @stack, $value;
$value = pop @stack;
# Array as queue
unshift @queue, $value;
$value = pop @queue;
Multidimensional Arrays
# Array of arrays
@matrix = (
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
);
# Accessing elements
$value = $matrix[1][2]; # 6
# Adding a row
push @matrix, [10, 11, 12];
Best Practices
- Use
push
andpop
instead of direct indexing when possible - Prefer
foreach
over C-stylefor
loops for array iteration - Use array slices for multiple element access
- Consider using references for complex data structures
- Use
exists
to check for array indices when needed