Perl Tutorial
Fundamentals
Input and Output
Control Flow
Arrays and Lists
Hash
Scalars
Strings
Object Oriented Programming in Perl
Subroutines
Regular Expressions
File Handling
Context Sensitivity
CGI Programming
Misc
Arrays are one of the fundamental data structures in Perl. There are several built-in functions to manipulate and inspect arrays. This tutorial will cover some of the essential array functions in Perl.
In Perl, arrays are ordered collections of scalars indexed by number, starting with 0. They are dynamic and can grow and shrink. Perl provides a plethora of functions to aid in working with arrays.
push
and pop
push
: Adds one or more elements to the end of an array.pop
: Removes and returns the last element of an array.my @arr = (1, 2, 3); push @arr, 4, 5; # @arr is now (1, 2, 3, 4, 5) my $last = pop @arr; # @arr is now (1, 2, 3, 4) and $last is 5
shift
and unshift
shift
: Removes and returns the first element of an array.unshift
: Adds one or more elements to the beginning of an array.my @arr = (2, 3, 4); unshift @arr, 1; # @arr is now (1, 2, 3, 4) my $first = shift @arr; # @arr is now (2, 3, 4) and $first is 1
splice
splice
can be used to replace a section of an array with other values or to extract a section of an array.
my @arr = (1, 2, 5, 6); splice @arr, 2, 0, (3, 4); # Inserts 3 and 4 before the 3rd element. @arr is now (1, 2, 3, 4, 5, 6) my @removed = splice @arr, 2, 2; # Removes 2 elements from the 3rd position. @arr is now (1, 2, 5, 6) and @removed is (3, 4)
sort
Sorts an array. By default, it sorts in standard string comparison order, but you can provide a custom comparison.
my @numbers = (4, 1, 8, 3); my @sorted = sort { $a <=> $b } @numbers; # @sorted is (1, 3, 4, 8)
reverse
Reverses the elements of an array.
my @arr = (1, 2, 3); my @reversed = reverse @arr; # @reversed is (3, 2, 1)
grep
Finds elements in an array that match a condition.
my @numbers = (1, 2, 3, 4, 5); my @evens = grep { $_ % 2 == 0 } @numbers; # @evens is (2, 4)
map
Transforms elements of an array.
my @numbers = (1, 2, 3); my @squared = map { $_**2 } @numbers; # @squared is (1, 4, 9)
scalar
Returns the number of elements in an array when used in scalar context.
my @arr = (1, 2, 3, 4); my $count = scalar @arr; # $count is 4
Perl's array functions offer a powerful set of tools to manipulate and inspect arrays. Understanding and mastering these functions enable effective and efficient array handling, which is pivotal for many tasks in Perl.
Manipulating arrays in Perl:
# Creating an array my @colors = ('red', 'green', 'blue'); # Accessing elements print "First color: $colors[0]\n"; # Modifying elements $colors[1] = 'yellow'; # Printing the modified array print "Modified colors: @colors\n";
Adding elements to arrays in Perl:
push
.my @fruits = ('apple', 'banana'); push @fruits, 'orange', 'grape'; # Printing the updated array print "Updated fruits: @fruits\n";
Removing elements from arrays in Perl:
pop
and shift
.my @numbers = (1, 2, 3, 4); pop @numbers; # Remove the last element shift @numbers; # Remove the first element # Printing the updated array print "Updated numbers: @numbers\n";
Searching arrays in Perl:
grep
and first
.my @names = ('Alice', 'Bob', 'Charlie'); my @found_names = grep { /b/i } @names; # Case-insensitive search # Printing the found names print "Found names: @found_names\n";
Sorting arrays in Perl:
sort
.my @scores = (30, 15, 50, 25); my @sorted_scores = sort @scores; # Printing the sorted array print "Sorted scores: @sorted_scores\n";
Iterating over arrays in Perl:
foreach
.my @animals = ('lion', 'elephant', 'zebra'); foreach my $animal (@animals) { print "Animal: $animal\n"; }
Slicing arrays in Perl:
my @numbers = (1, 2, 3, 4, 5); my @subset = @numbers[1..3]; # Printing the sliced array print "Sliced subset: @subset\n";
Joining arrays in Perl:
join
.my @fruits = ('apple', 'orange', 'banana'); my $fruit_string = join(', ', @fruits); # Printing the joined string print "Fruits: $fruit_string\n";
Array functions vs. array operators in Perl:
my @numbers = (3, 1, 4, 1, 5, 9); my $count = scalar @numbers; # Function my $sum = 0; # Using an operator foreach my $num (@numbers) { $sum += $num; } # Printing the count and sum print "Count: $count, Sum: $sum\n";