perl核心模块解析(二)List
perl核心模块解析(二) ? ?List 详细文档链接如下 源自:http://blog.csdn.net/jonathanxqs 转自:http://perldoc.perl.org/List/Util.html
http://www.xker.com/page/e2015/06/193925.html???
在Perl中有一些专门用于处理列表数据的模块,比如说List::Util模块,该模块包含在标准库中,能提供各种高效的常见列表处理工具。因其用C语言来实现,速度一般都挺快! 【例01】扫描符合条件的某个列表,并取出第一个符合条件的
代码如下:
use 5.010;?? my @names? = qw(Wendy Jerry Betty Wendy Alice);?? foreach? (@names) {?? ??? if (/bWendyb/i) {?? ??????? $match = $_;?? ??????? last;?? ??? }?? }?? say $match;?????? 如果改用List::Util模块提供的first子程序,就要简单的多
代码如下:
use List::Util qw(first);?? my $match = first {/bWendyb/i} @names;? #找到第一个Wendy即终止?? 如果换成数字的话,比如要求 【例02】求1到1000之间的和 常规做法: 代码如下: use 5.010;?? my $total = 0;?? foreach (1..1000) {?? ??? $total += $_;?? }?? say $total;?????????????? #结果500500?? 如果改用List::Util模块提供的sum子程序,同样很简单: 代码如下: use List::Util qw(sum);?? my $total = sum(1..1000); #结果500500?? 【例03】求一组数字的最大值与最小值. 代码如下: #! /usr/bin/perl;???? use utf8;???? ???? sub max {???? ?? my($max_so_far) = shift @_; #数组中第一个值,暂时当成最大值。???? ?? foreach(@_){??????????????? #遍历数组@_???? ?? if($_>$max_so_far){???????? #看其它元素是否有比$max_so_far大的值。??? ??? $max_so_far = $_;}???????? #如果有话,更新最大值变量???? ?? }???? ?? $max_so_far;???? }???? ???? my $_MaxData = &max(2,3,8,5,10);???? print $_MaxData;?????????????? #结果为10?? 如果改用List::Util模块提供的max子程序,则非常简单: 代码如下: use List::Util qw(max);?? my $max = max(2,10);?? print $max;???????????????? #结果为10?? 同样道理,使用List::Util模块提供的min子程序,可求最小值: 代码如下: use List::Util qw(min);?? my $min = min(2,10);? #最小值为2?? 【例04】对一组字符串进行排序 如果用常规方法的话,必须按顺序一个个进行比较,用List::Util的maxstr子程序可以轻松实现: 代码如下: use List::Util qw(maxstr);?? my? $max_str = maxstr( qw/Jerry Betty Alice Fred Barney jerry/ );?? print $max_str;?? 【例05】对列表中的元素随机排序 如果用常规方法的话,很难实现,而用List::Util中的shuffle子程序,则非常简单,一条命令搞定! 代码如下: use List::Util qw(shuffle);?? my @shuffled_nums = shuffle(1..10);?? # 3 9 8 5 6 4 1 10 2 7?????????????????? my @shuffled_name = shuffle('A'..'G');# F E G A B D??? 【例06】检查列表中是否没有某个元素,或者有任何元素,或者所有元素都符合条件。支持类似grep语法 如果用常规方法的话,很难实现,而用List::MoreUtils同样很容易实现,代码如下: 代码如下: use List::MoreUtils qw(none any all);?? my @numbers = qw(7 4 1 3 78);?? if (none {$_ > 100} @numbers) {print "No elements over 100n"; }??? elsif (any {$_ > 50}@numbers) {print "Some elements over 50n";}?? elsif (all {$_ <10} @numbers) {print "All elements < 10n";}?? NOTE: List::MoreUtils非自带的模块,需要下载. 【例07】同时处理多个姓名列表,每次取出2位 代码如下: use List::MoreUtils qw(natatime);??? my @names_1 = qw(Alice Bob Carly);?? my @names_2 = qw(David Edward Foo);?? my $names?? = natatime(2,@names_1,@names_2);? #natatim (N at a time:同时处理N组)有多个列表的话往里面放就好了?? while (my @name = $names->()) {???????????????? #遍历,方便后面输出? ??? print "Got @namen";?? }?? #输出结果?? Got Alice Bob?? Got Carly David?? Got Edward Foo??
【例08】合并多个列表为一个列表
代码如下:
use List::MoreUtils qw(mesh);?? my @array_1 = 'A' .. 'D';?? my @array_2 = 1 .. 4;?? my @array_3 = qw( jerry alice wendy );?? my @array_new = mesh(@array_1,@array_2,@array_3);?? print @array_new;?? #输出结果:?? A=>1=>jerry=> B=>2=>alice=> C=>3=>wendy=> D=>4? 第1次: 第一个列表中取A,第二个列表中取出1,第三个列表中取出jerry
代码如下:
use v5.10;?? use List::MoreUtils qw(:all);?? ?? my @list = qw/This is a list/;?? insert_after {$_ eq 'a'} "longer" => @list;?? print @list;? #This is a longer list? 【例10】对两个列表进行操作,第一个列表平方后,和第二个列表求和 可以用List::MoreUtils中的pairwise子程序 代码如下: use v5.10;?? use List::MoreUtils qw(:all);?? ?? @m = (1..4);?? @n = (100..103);?? @x = pairwise { ($a*$a) + $b } @m,@n;? #101 105 111 119?? 【总结】 如果上面的方式用普通的方法实现,需要多次循环遍历,很麻烦,而将复杂的算法,数据结构用C来实现并封装在List模块中,则可以实现很多看似复杂的功能,从中也可以看出Perl的强大之处在于CPAN,有众多的模块支撑!
【Reference】
List::Util
NAMEList::Util - A selection of general-utility list subroutines SYNOPSISDESCRIPTION By default? The following set of functions all reduce a list down to a single value. $result = reduce { BLOCK } @list Reduces? Returns the result of the last call to the? The following examples all demonstrate how? $foo = reduce { defined($a) ? $a : If your algorithm requires that? @values; # sum with 0 identity value The remaining list-reduction functions are all specialisations of this generic idea. anymy $bool = any { BLOCK @list; Since version 1.33. Similar to? Many cases of using? if( { length > 10 @strings { all Similar to?any,except that it requires all elements of the? none ?returns true only if no value in the?@list ?causes theBLOCK ?to return true,and?notall ?returns true only if not all of the values do.
first
$val = first first?returns the first element where the result from? $num = max Returns the entry in the list with the highest numerical value. If the list is empty then?
maxstr
$str = maxstr Similar to?max,but treats all the entries in the list as strings and returns the highest string as defined by the?min min max?but returns the entry in the list with the lowest numerical value. If the list is empty then?
minstr
minstr Similar to?min,but treats all the entries in the list as strings and returns the lowest string as defined by the?
product
product Since version 1.35. $num_or_undef = sum Returns the numerical sum of all the elements in?sum0 sum0 Since version 1.26. The following are additions that have been requested,but I have been reluctant to add due to them being very simple to implement in perl # How many elements are true Scalar::Util,?List::MoreUtils COPYRIGHTCopyright (c) 1997-2007 Graham Barr <gbarr@pobox.com>. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. Recent additions and current maintenance by Paul Evans,<leonerd@leonerd.org.uk>. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |