Perl List and its Types

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl
  
# 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); 

chevron_right


 
Lists are of multiple types as described below:

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

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #!/usr/bin/perl
      
    # 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";

    chevron_right

    
    

    Output:

    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 :

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    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 :

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #!/usr/bin/perl
      
    # 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"

    chevron_right

    
    

    Output:

    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 :

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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.
Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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 :

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl
  
# 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";

chevron_right


Output:

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.
Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl
  
# 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";

chevron_right


Output:

Sliced List: 3 For 5


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.