Updated April 5, 2023
Introduction to Perl list
The Perl list is the collection of single characters and numbers in the form of an array. It is the group of the scalar values and used (,) comma symbol to binding all single variables. The list is assigned as an array in the Perl language and makes requires operations of the scalar values. The list is the combination of scalar value which means single string value and single number. It was grouping the different data type’s values using the comma symbol and modified them as per the user’s requirements.
Syntax
The simple list syntax is below.
- The Empty list syntax is below.
@perl_empty_list = ( );
- The empty Perl list returns the null or empty value.
- The Numerical list syntax is below.
@perl_numerical_list = (9, 8, 7, 6, 5, 4, 3, 2, 1);
- The numerical list returns only the numbers.
- The (,) comma symbol binding the single numbers to create the list.
- The String Perl list syntax is below.
@perl_string_list = ("good", "to", "learn", "new", "technical", "skill", "like", "Perl");
- The string Perl list returns only the characters.
- The (” “) double-quotes symbol used for string and (,) comma symbol binding the single character to create the list.
The complex list syntax is below.
@complex_perl_list = ("good", "to", "learn", "new", "technical", "skill", 401, 404, 500, 501 );
- The complex Perl list returns numbers as well as characters.
- The (” “) double-quotes symbol is used for the string but not required for numbers.
- The (,) comma symbol binding the single character to create the list.
The flattening Perl list syntax is below.
@initial_perl_list = (1, 2, 8, 9, 5);
@modified_perl_list = ("learn", "new", "technical", "skill", @initial_perl_list);
- The flattening Perl list is added the old list with the new list using syntax.
- The (” “) double-quotes symbol is using for the string but not required for number.
- The (,) comma symbol binding the single character to create the list.
How to work with a list in Perl?
- The download the Perl software and install it in your operating system of the device.
https://www.Perl.org/ or http://strawberryPerl.com/ are commonly using Perl software websites.
- Make a file with the Perl extension in the device and save the file in the command line path.
Example: helloo.pl or first pearl.pl
- Create the list variable and initialize with the scalar values.
@perl_numerical_list = (9, 8, 7, 6, 5, 4, 3, 2, 1);
- Modifies the Perl list as per user requirement using the List method and function.
- Return the list to the output screen.
print "Perl list is here: @perl_numerical_list ";
- The combine the working step together is below.
@perl_numerical_list = (9, 8, 7, 6, 5, 4, 3, 2, 1);
@perl_string_list = ("good", "to", "learn", "new", "technical", "skill", "like", "Perl");
@perl_empty_list = ( );
print "Perl list is here: @perl_numerical_list ";
print "Perl list is here: @perl_string_list ";
print "Perl list is here: @perl_empty_list ";
Examples
Here are the following examples mention below
Example #1
The different types of list Example and output.
Code:
@perl_numerical_list = (9, 8, 7, 6, 5, 4, 3, 2, 1);
print "Perl numerical list is here: @perl_numerical_list \n ";
@perl_string_list = ("good", "to", "learn", "new", "technical", "skill", "like", "Perl");
print "Perl string list is here: @perl_string_list \n ";
@perl_empty_list = ( );
print "Perl empty list is here: @perl_empty_list \n ";
@perl_complex_list = (9, 8, 7, "good", "to", "learn", "new", "technical", "skill", "like", "Perl", 6, 5);
print "Perl complex list is here: @perl_complex_list \n ";
@initial_perl_list = (1, 2, 8, 9, 5);
@fluttening_perl_list = ("learn", "new", "technical", "skill", @initial_perl_list);
print "Perl complex list is here: @fluttening_perl_list \n ";
Output:
Description:
- The first list numerical list and the second list is a simple string list.
- The third list is the empty list and returns a null value.
- The combination of the string and numerical values called the complex list.
- The list is containing the simple or complex list is called a fluttening Perl list.
Example #2
The list with a range of values, example and output.
Code:
@perl_numerical_list = (1 .. 9);
print "Perl numerical list is here: @perl_numerical_list \n ";
@perl_numerical_list1 = (9 .. 1);
print "reverse numerical range Perl list is here: @perl_numerical_list1 \n ";
@perl_uppercase_list = ("D" .. "N");
print "Perl string list is here: @perl_uppercase_list \n ";
@perl_lowercase_list = ("a" .. "j" );
print "Perl empty list is here: @perl_lowercase_list \n ";
@perl_complex_list = (1..4, "a" .. "e");
print "Perl complex list is here: @perl_complex_list \n ";
Output:
Description
- The first list displays 1 to 9 numbers as per the range requirement in the list.
- The reverse range is undefined in the list; users can show it in the second list output.
- The uppercase and lowercase characters create the listed range.
- Users may use numerical, and character ranges together in the list.
Example #3
The different types of methods in the list example and output.
Code:
@perl_numerical_list = (9, 8, 7, 6, 5, 4, 3, 2, 1);
print " Accesing single element in the Perl list: $perl_numerical_list[5] \n ";
@perl_string_list = ("good", "to", "learn", "new", "technical", "skill", "like", "Perl");
@perl_string_list1 = @perl_string_list[3, 4, 7, 0];
print "Slicing element in the Perl list: @perl_string_list1 \n ";
@perl_string_list2 = ("good", "to", "learn", "new", "technical", "skill", "like", "Perl");
@perl_string_list3 = @perl_string_list2[2 .. 5 ];
print "Perl empty list is here: @perl_string_list3 \n ";
Output:
Description:
- The @perl_numerical_list variable is using for accessing the single value of the list.
- The second list is using for slicing the list as per the user’s requirement.
- The third list is using for combining the range and slicing the list method together.
Example #4
The reverse function of the method in the list example and output.
Code:
@perl_numerical_list = (9, 8, 7, 6, 5, 4, 3, 2, 1);
print "Perl list: @perl_numerical_list \n ";
@perl_reverse_list = reverse(@perl_numerical_list);
print "Reverse value of the Perl list: @perl_reverse_list \n ";
Output:
Description:
- The above example using the reverse function to create a reverse list.
- The reverse(@perl_list); function is using in the list.
Conclusion
It is useful for grouping all types of a single value and modifies as per the user’s requirement. The list is user-friendly, and easy to access the coding data for the programmer.
Recommended Articles
This is a guide to the Perl list. Here we discuss How to work with a list in Perl and Examples along with the codes and outputs. You may also have a look at the following articles to learn more –