Skip to content
Related Articles

Related Articles

Perl List and its Types
  • Last Updated : 26 Mar, 2019
Introduction to Lists

A list is a collection of scalar values. We can access the elements of a list using indexes. Index starts with 0 (0th index refers to the first element of the list). We use parenthesis and comma operators to construct a list. In Perl, scalar variables start with a $ symbol whereas list variables start with @ symbol.

Important Note : List in perl is not a data structure. They are only some subexpressions/expressions in code. They are typically assigned to an array.

# Empty List assigned to an array
# Note that the expression () is a 
# list and "empty_list" variable is an
# array variable.
@empty_list = (); 
# Note that the expression (1, 2, 3) is a 
# list and "integer_list" variable is an
# array variable.
@integer_list = (1, 2, 3); 

Lists are of multiple types as described below:

  • Simple Lists: A list with same datatypes is referred to as a Simple List
    Example :

    # Empty List assigned to an array
    @empty_list = (); 
    # List of integers
    @integer_list = (1, 2, 3); 
    # List of strings assigned to an array
    @string_list = ("Geeks", "for", "Geeks"); 
    print "Empty list: @empty_list\n";
    print "Integer list: @integer_list\n";
    print "String list: @string_list\n";


    Empty list: 
    Integer list: 1 2 3
    String list: Geeks for Geeks
  • Complex Lists: A list may contain various different datatypes. These type of lists are referred to as Complex Lists.
    Example :

    # List of strings and integers assigned to an array
    @complex_list = (1, 2, "Geeks", "for", "Geeks"); 
    # printing this List
    print "Complex List: @complex_list";


    Complex List: 1 2 Geeks for Geeks
  • Flattening List: A list may contain another list within it but Perl will flatten the internal list and the elements of this list will be treated as the elements of the outer list.
    Example :

    # Defining Internal list as an array
    @Internal_list = (5, 6, 7);
    # Defining External list.
    @External_list = (1, "Geeks", 3, "For", @Internal_list); 
    # Printing Flattening list
    print "Printing list within list: @External_list"


    Printing list within list: 1 Geeks 3 For 5 6 7
Accessing List Elements

List elements can be accessed with the use of a scalar variable. While accessing a List element, $ is used, because a scalar variable in Perl is accessed with the use of $ symbol.
Example :

# Defining a list
@List = (1, 2, 3, 4, 5);
# Accessing list element
print "Second element of List is: $List[2]"


Second element of List is: 3

Slicing a List

Slicing a list in Perl can be done by giving comma(,) separated index values to another list.

# Defining 1st List
@list1 = (1, "Geeks", 3, "For", 5);
# Defining 2nd List
@list2 = @list1[1, 2, 4];
# Printing Sliced List
print "Sliced List: @list2";


Sliced List: Geeks 3 5

Defining Range in a List

Range operator in Perl is used as a short way to create a list. When used with list, the range operator simplifies the process of creating a list with contiguous sequences of numbers and letters. The range operator can also be used for slicing the list.

Syntax: leftValue..rightValue

Note: If leftValue is greater than rightValue then it will create an empty list otherwise it will contiguously allocate values from leftValue till rightValue.

Examples :

# Defining list with range of a to j 
@x = ("a".."j");
# Defining list with range of 1 to 15
@y = (1..15);
# Defining list with range of A to J
@z = ("A".."J");
# Priting these lists
print "List with elements from a to j: @x\n";
print "List with elements from 1 to 15: @y\n";
print "List with elements from A to J: @z";


List with elements from a to j: a b c d e f g h i j
List with elements from 1 to 15: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
List with elements from A to J: A B C D E F G H I J

Combining Ranges and Slices:
Range and slice operators can be combined together to perform slicing operation on a list.

# Defining a list of elements
@x = ("Geeks", 2, 3, "For", 5);
# Use of Range and slice operator
@z = @x[2..4];
# Printing the sliced List
print "Sliced List: @z";


Sliced List: 3 For 5

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :