Perl

Uses of Perl List

The list is a data structure that is used in Perl to store multiple scalar values. It works similarly to the array variables. The “@” symbol is used to declare the list variable and the index of the list variable stars from 0 like the array. The difference between a list and an array is that an array can contain a list as an element but the list can store the scalar data only. The list can be used to store the ordered sequence of values. The methods of declaring, accessing, and printing the list data are shown in this tutorial.

Syntax

The syntax for declaring the list variables in Perl is given in the following. The list variable without any value can be declared which is called an “empty list” and the values can be added to the list later. The values of the list can be assigned at the time of declaration.

@list_variable = ();

Or

@list_variable = (value1 value2 ….valueN);

Different Perl List Examples

Different ways of declaring and accessing the list variables in Perl are shown in this part of the tutorial.

Example 1: List of Numeric Data

In this example, a list of numeric data is declared and the list is printed using the “say”, “join”, and “Dumper” functions.

#!/usr/bin/perl
use warnings;
use strict;
use 5.34.0;
use Data::Dumper;

#Declare a list of numeric data
my @num_list = ( 6, 3, 8, 1, 5 );
say "The content of the list is:";

#Print the list using different functions
say join(', ', @num_list);
print join(', ', @num_list), "\n";
print Dumper (\@num_list);

Output:

The following output appears after executing the script. Both “say” and “join” functions generated the same output here. The “Dumper” function generates the output using the $VAR1 variable:

Example 2: List of String Data

In this example, an empty list is declared first and four string values are added to the list later. Like in the previous example, the list is printed using the “say” and “join” functions.

#!/usr/bin/perl
use warnings;
use strict;
use 5.34.0;

#Declare an empty list
my @str_list = ();

#Add four values to the list
$str_list[0] = "Rose";
$str_list[1] = "Lily";
$str_list[2] = "Sun Flower";
$str_list[3] = "Daffodil";

#Print the list using different functions
say join(', ', @str_list);
print join(', ', @str_list), "\n";

Output:

The following output appears after executing the script. Both “say” and “join” functions generated the same output here.

Example 3: Generate a List from the Range of Values

The data are stored in the sequential order in the list if the range is used to assign the values in the list. In the following example, two list variables are declared using a range of characters and a range of numbers. The first list variable contains four characters from “a” to “d” and the second list variable contains three numbers from 1 to 3. The nested “for” loop is used in the script to print a series of values using the values of both lists.

#!/usr/bin/perl
use warnings;
use strict;
use 5.34.0;

#Declare a list of 4 characters
my @char_list = ('a'..'d');

#Declare a list of 3 numbers
my @num_list = (1..3);

say "The output is:";
#Declare the outer loop to read the character array
my @num_list = (1..3);

say "The output is:";
#Declare the outer loop to read the character array
for (my $i = 0; $i <= $#char_list; $i++)
{
      #Declare the inner loop to read the number array
      for (my $j = 0; $j <= $#num_list; $j++)
      {
            #Print the concatenated array values
            print "$char_list[$i]$num_list[$j] ";
      }
}
print "\n";

Output:

The following output appears after executing the script. The first list has four values and the second list has three values. So, 4×3=12 values are printed in the output:

Example 4: Insert New Elements into the List

Multiple functions exist in Perl to insert new values in the list. The push() function is one of them. In the following example, a list of five fractional data is declared. Next, two new values are added using the push() function. The list is printed before and after adding the values.

#!/usr/bin/perl
use warnings;
use strict;
use 5.34.0;
use Data::Dumper;

#Declare a list of decimal number
my @num_list = ( 4.7, 3.8, 2.9, 5.7, 1.6 );
say "The content of the list before insert:";

#Print the list using dumper
print Dumper (\@num_list);

#Add an element to the list
push(@num_list, 5.9);

#Add another element to the list
push(@num_list, 4.2);

say "The content of the list after insert:";
#Print the list using Dumper after inserting two elements
print Dumper (\@num_list)

Output:

The following output appears after executing the script. According to the output, the list contains seven values after adding new values:

Conclusion

The methods of declaring, printing, and adding new values in the Perl list are shown in this tutorial using multiple examples for the new Perl users.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.