Perl | List Functions - GeeksforGeeks

07 May, 2019
A list in Perl 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.List provides various pre-defined Functions to perform operations with ease. Some of these Functions are as below:
join() functionjoin() function is used to combine the elements of a List into a single string with the use of a separator provided to separate each element. This function returns the joined string. A separator can work only between the pairs. A separator can’t be placed at either end of the string. In order to join the strings without a separator, an empty parameter is passed to the function.Syntax: join(Separator, List)Parameter:Separator: provided to separate each element while joiningList: to be converted to single StringReturns: a joined StringExample :#!/usr/bin/perl  # Initializing list with alphabets A to Z@list = (A..Z);  # Printing the original listprint "List: @list\n";  # Using join function introducing# hyphen between each alphabetsprint "\nString after join operation:\n";print join("-", @list);Output:List: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

String after join operation:
A-B-C-D-E-F-G-H-I-J-K-L-M-N-O-P-Q-R-S-T-U-V-W-X-Y-Z

Syntax: join(Separator, List)
Parameter:
Separator: provided to separate each element while joining
List: to be converted to single String
Returns: a joined String
Example :
#!/usr/bin/perl  # Initializing list with alphabets A to Z@list = (A..Z);  # Printing the original listprint "List: @list\n";  # Using join function introducing# hyphen between each alphabetsprint "\nString after join operation:\n";print join("-", @list);
Output:
List: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

String after join operation:
A-B-C-D-E-F-G-H-I-J-K-L-M-N-O-P-Q-R-S-T-U-V-W-X-Y-Z

reverse() functionReverse() function in Perl returns the elements of List in reverse order in a list context. While in a scalar context, it returns a concatenated string of the values of List, with all characters in opposite order. It returns String in Scalar Context and List in List Context.Syntax: reverse ListParameter:List: list to be reversedReturns: elements in reverse orderExample:# Initializing a list@list = ("Raj", "E123", 12000);  # Reversing the list@rname = reverse(@list);  # Printing the reversed listprint "Reversed list is @rname";  # Initializing a scalar$string = "GeeksforGeeks";  # Reversing a scalar$r = reverse($string);print "\nReversed string is $r";Output:Reversed list is 12000 E123 Raj
Reversed string is skeeGrofskeeG

Syntax: reverse List
Parameter:List: list to be reversed
Returns: elements in reverse order
Example:
# Initializing a list@list = ("Raj", "E123", 12000);  # Reversing the list@rname = reverse(@list);  # Printing the reversed listprint "Reversed list is @rname";  # Initializing a scalar$string = "GeeksforGeeks";  # Reversing a scalar$r = reverse($string);print "\nReversed string is $r";
Output:
Reversed list is 12000 E123 Raj
Reversed string is skeeGrofskeeG

map() functionmap() function in Perl evaluates the operator provided as a parameter for each element of List. For each iteration, $_ holds the value of the current element, which can also be assigned to allow the value of the element to be updated. map() function runs an expression on each element of an array and returns a new array with the updated results. It returns the total number of elements generated in scalar context and list of values in list context.Syntax: map(operation, List)Parameter:operation: to be performed on list elementsList: whose elements need to be changedExample :# Initializing a list@Dept = ('comp', 'inft', 'extc', 'mech');  # Converting first character capital@upd1 = map(ucfirst, @Dept);  # Printing listprint "List with First char capital: ";foreach $i (@upd1) {   print "$i, ";}  # Converting all characters capital@upd2 = map(uc, @Dept);  # Printing listprint "\nList with all char capital: ";foreach $i (@upd2) {   print "$i, ";}Output:List with First char capital: Comp, Inft, Extc, Mech, 
List with all char capital: COMP, INFT, EXTC, MECH, 
Syntax: map(operation, List)Parameter:
operation: to be performed on list elements
List: whose elements need to be changed
Example :
# Initializing a list@Dept = ('comp', 'inft', 'extc', 'mech');  # Converting first character capital@upd1 = map(ucfirst, @Dept);  # Printing listprint "List with First char capital: ";foreach $i (@upd1) {   print "$i, ";}  # Converting all characters capital@upd2 = map(uc, @Dept);  # Printing listprint "\nList with all char capital: ";foreach $i (@upd2) {   print "$i, ";}
Output:
List with First char capital: Comp, Inft, Extc, Mech, 
List with all char capital: COMP, INFT, EXTC, MECH, 
sort() functionsort() function in Perl is used to arrange the List according to the condition of sorting specified to the function. If no condition is specified, then it sorts according to normal alphabetical sequence.If a condition is specified then the function sorts the List according to the condition.Syntax: sort(condition, List)Example# Initializing two lists@country = ('India', 'Qatar', 'Bangladesh', 'France', 'Italy');@capital = ('Delhi', 'Lahore', 'Dhaka', 'Paris', 'Rome');  # Printing countries in sorted orderprint"Countries in sorted order: \n";print sort @country;print "\n";  # Printing sorted country and capital valuesprint "\nCombining both the lists in sorted order:\n";print sort @country, @capital;print "\n";  # Initializing a list with number@list = (19, 4, 54, 33, 99, 2);  # Sorting in descending order@s = sort{$b <=> $a} @list;print "\nPrinting numbers in sorted order:\n";foreach $i(@s){    print "$i, ";}Output:Countries in sorted order: 
BangladeshFranceIndiaItalyQatar

Combining both the lists in sorted order:
BangladeshDelhiDhakaFranceIndiaItalyLahoreParisQatarRome

Printing numbers in sorted order:
99, 54, 33, 19, 4, 2, 
Syntax: sort(condition, List)
Example
# Initializing two lists@country = ('India', 'Qatar', 'Bangladesh', 'France', 'Italy');@capital = ('Delhi', 'Lahore', 'Dhaka', 'Paris', 'Rome');  # Printing countries in sorted orderprint"Countries in sorted order: \n";print sort @country;print "\n";  # Printing sorted country and capital valuesprint "\nCombining both the lists in sorted order:\n";print sort @country, @capital;print "\n";  # Initializing a list with number@list = (19, 4, 54, 33, 99, 2);  # Sorting in descending order@s = sort{$b <=> $a} @list;print "\nPrinting numbers in sorted order:\n";foreach $i(@s){    print "$i, ";}
Output:
Countries in sorted order: 
BangladeshFranceIndiaItalyQatar

Combining both the lists in sorted order:
BangladeshDelhiDhakaFranceIndiaItalyLahoreParisQatarRome

Printing numbers in sorted order:
99, 54, 33, 19, 4, 2, 
Perl-Arrays
perl-list
Perl-List-Functions
Picked
Perl
Perl
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Perl Tutorial - Learn Perl With Examples
Perl | Data Types
Perl | Multidimensional Hashes
Perl | Basic Syntax of a Perl Program
Perl | Decision Making (if, if-else, Nested–if, if-elsif ladder, unless, unless-else, unless-elsif)
Perl | File Handling Introduction
Perl | Opening and Reading a File
Perl | Writing to a File
Perl | defined() Function
Perl | ne operator