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
In Perl, a list is an ordered set of scalars. It's one of the foundational data types in Perl, often utilized in arrays and hashes. Lists in Perl are versatile, and understanding how to use them effectively is crucial.
A list is an ordered set of values, separated by commas:
(1, 2, 3, 4, 5)
Lists are often assigned to arrays:
my @numbers = (1, 2, 3, 4, 5); my @fruits = ("apple", "banana", "cherry");
You can create lists of sequential numbers or letters using the range operator (..
):
my @nums = (1..10); # (1, 2, 3, ..., 10) my @letters = ('a'..'z'); # (a, b, c, ..., z)
You can access individual elements from a list when it's assigned to an array:
my @colors = ("red", "green", "blue"); print $colors[0]; # prints "red"
When a list is evaluated in scalar context, it returns the last element:
my $value = (1, 2, 3); # $value is 3
qw
is a handy way to create a list of strings. It stands for "quoted words".
my @days = qw(Monday Tuesday Wednesday Thursday Friday);
It's the same as writing:
my @days = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday");
Lists can contain mixed data types:
my @mixed = (1, "apple", 2.5, "banana");
Lists can be nested, but remember that a true multi-dimensional array doesn't exist in Perl. Instead, references are used to simulate them:
my @nested = (1, 2, 3, [4, 5, 6], 7, 8);
In this example, the list contains another list at the fourth position.
You can extract multiple values from a list using slices:
my @numbers = (1..10); my @slice = @numbers[1, 3, 5]; # (2, 4, 6)
Perl offers a variety of functions that operate on lists:
sort
: sorts the listreverse
: reverses the listpush
: adds elements to the endpop
: removes the last elementshift
: removes the first elementunshift
: adds elements to the beginningmy @nums = (4, 2, 8, 1); @nums = sort @nums; # (1, 2, 4, 8)
In Perl, lists play a pivotal role in data organization and manipulation. Understanding how to work with lists is crucial to becoming proficient in Perl, as they underlie the more complex data structures and are frequently used in various operations.
Types of lists in Perl:
my @numbers = (1, 2, 3, 4, 5); my @fruits = ('apple', 'banana', 'orange'); my @mixed = (1, 'apple', 3.14, 'orange');
Creating and manipulating lists in Perl:
my @colors = ('red', 'green', 'blue'); # Adding elements push @colors, 'yellow'; # Removing elements pop @colors; # Slicing my @subset = @colors[1, 2];
Scalar vs. list context in Perl:
my @numbers = (1, 2, 3); my $last_number = @numbers; # $last_number is 3
Anonymous arrays in Perl:
my $ref_to_array = [1, 2, 3];
List assignment in Perl:
my ($first, $second, $third) = (1, 2, 3);
List functions and operations in Perl:
map
, grep
, sort
, and more.my @numbers = (3, 1, 4, 1, 5, 9); my @squared = map { $_ ** 2 } @numbers; my @evens = grep { $_ % 2 == 0 } @numbers; my @sorted = sort @numbers;
List literals in Perl:
qw()
or qw//
constructs.my @colors = ('red', 'green', 'blue'); my @numbers = (1..10);
Using references with Perl lists:
my $ref_to_array = [1, 2, 3]; my $array_from_ref = @{$ref_to_array};
Lists vs. arrays in Perl:
my @array = (1, 2, 3); my $array_ref = \@array;