珀尔 |列出函数
Perl 中的列表是标量值的集合。我们可以使用索引访问列表的元素。索引从 0 开始(第 0 个索引是指列表的第一个元素)。我们使用括号和逗号运算符来构造一个列表。在 Perl 中,标量变量以 $ 符号开头,而列表变量以 @ 符号开头。
List 提供各种预定义的函数来轻松执行操作。其中一些功能如下:
- 加入()函数
join()函数用于将 List 的元素组合成单个字符串,并使用提供的分隔符来分隔每个元素。此函数返回连接的字符串。分隔符只能在对之间起作用。分隔符不能放在字符串的任何一端。为了在没有分隔符的情况下连接字符串,将一个空参数传递给函数。Syntax: join(Separator, List)
Parameter:
- Separator: provided to separate each element while joining
- List: to be converted to single String
Returns: a joined String
例子 :
#!/usr/bin/perl # Initializing list with alphabets A to Z @list = (A..Z); # Printing the original list print "List: @list\n"; # Using join function introducing # hyphen between each alphabets print "\nString after join operation:\n"; print join("-", @list);
输出:
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
- 反向()函数
Perl 中的 Reverse()函数在列表上下文中以相反的顺序返回 List 的元素。在标量上下文中,它返回 List 值的串联字符串,所有字符的顺序相反。它在标量上下文中返回字符串,在列表上下文中返回列表。Syntax: reverse List
Parameter:
List: list to be reversedReturns: elements in reverse order
例子:
# Initializing a list @list = ("Raj", "E123", 12000); # Reversing the list @rname = reverse(@list); # Printing the reversed list print "Reversed list is @rname"; # Initializing a scalar $string = "GeeksforGeeks"; # Reversing a scalar $r = reverse($string); print "\nReversed string is $r";
输出:
Reversed list is 12000 E123 Raj Reversed string is skeeGrofskeeG
- 地图()函数
Perl 中的 map()函数计算作为参数提供给 List 的每个元素的运算符。对于每次迭代,$_ 保存当前元素的值,也可以分配它以允许更新元素的值。 map()函数对数组的每个元素运行一个表达式,并返回一个包含更新结果的新数组。它返回在标量上下文中生成的元素总数和列表上下文中的值列表。Syntax: map(operation, List)
Parameter:- operation: to be performed on list elements
- List: whose elements need to be changed
例子 :
# Initializing a list @Dept = ('comp', 'inft', 'extc', 'mech'); # Converting first character capital @upd1 = map(ucfirst, @Dept); # Printing list print "List with First char capital: "; foreach $i (@upd1) { print "$i, "; } # Converting all characters capital @upd2 = map(uc, @Dept); # Printing list print "\nList with all char capital: "; foreach $i (@upd2) { print "$i, "; }
输出:
List with First char capital: Comp, Inft, Extc, Mech, List with all char capital: COMP, INFT, EXTC, MECH,
- 排序()函数
Perl 中的 sort()函数用于根据函数指定的排序条件对 List 进行排列。如果没有指定条件,则按照正常的字母顺序排序。
如果指定了条件,则函数根据条件对列表进行排序。Syntax: sort(condition, List)
例子
# Initializing two lists @country = ('India', 'Qatar', 'Bangladesh', 'France', 'Italy'); @capital = ('Delhi', 'Lahore', 'Dhaka', 'Paris', 'Rome'); # Printing countries in sorted order print"Countries in sorted order: \n"; print sort @country; print "\n"; # Printing sorted country and capital values print "\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, "; }
输出:
Countries in sorted order: BangladeshFranceIndiaItalyQatar Combining both the lists in sorted order: BangladeshDelhiDhakaFranceIndiaItalyLahoreParisQatarRome Printing numbers in sorted order: 99, 54, 33, 19, 4, 2,