perl核心模块解析(二) List::Util
详细文档链接如下
源自: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 (/\bWendy\b/i) {
$match = $_;
last;
}
}
say $match;
如果改用List::Util模块提供的first子程序,就要简单的多
代码如下:
use List::Util qw(first);
my $match = first {/\bWendy\b/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, 3, 8, 5, 10);
print $max; #结果为10
同样道理,使用List::Util模块提供的min子程序,可求最小值:
代码如下:
use List::Util qw(min);
my $min = min(2, 3, 8, 5, 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 100\n"; }
elsif (any {$_ > 50}@numbers) {print "Some elements over 50\n";}
elsif (all {$_ <10} @numbers) {print "All elements < 10\n";}
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 @name\n";
}
#输出结果
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
第2次: 第一个列表中取B, 第二个列表中取出2,第三个列表中取出alice
......
依次类推!
【例09】往指定的字符串中加字符
可以用List::MoreUtils中的insert_after子程序
代码如下:
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
- NAME
- SYNOPSIS
- DESCRIPTION
- LIST-REDUCTION FUNCTIONS
- KEY/VALUE PAIR LIST FUNCTIONS
- OTHER FUNCTIONS
- KNOWN BUGS
- SUGGESTED ADDITIONS
- SEE ALSO
- COPYRIGHT
NAME
List::Util - A selection of general-utility list subroutines
SYNOPSIS
- use List::Util qw(first max maxstr min minstr reduce shuffle sum);
DESCRIPTION
List::Util
contains a selection of subroutines that people have expressed would be nice to have
in the perl core, but the usage would not really be high enough to warrant the use of a keyword, and the size so small such that being individual extensions would be wasteful.
By default List::Util
does not export any subroutines.
LIST-REDUCTION FUNCTIONS
The following set of functions all reduce a list down to a single value.
$result = reduce { BLOCK } @list
Reduces @list
by calling BLOCK
in
a scalar context multiple times, setting $a
and $b
each
time. The first call will be with $a
and $b
set
to the first two elements of the list, subsequent calls will be done by setting $a
to the
result of the previous call and $b
to the next element in the list.
Returns the result of the last call to the BLOCK
. If @list
is
empty then undef
is
returned. If @list
only contains one element then that element is returned and BLOCK
is
not executed.
The following examples all demonstrate how reduce
could be used to implement the other list-reduction
functions in this module. (They are not in fact implemented like this, but instead in a more efficient manner in individual C functions).
- $foo = reduce { defined($a) ? $a :
- $code->(local $_ = $b) ? $b :
- undef } undef, @list # first
- $foo = reduce { $a > $b ? $a : $b } 1..10 # max
- $foo = reduce { $a gt $b ? $a : $b } ‘A‘..‘Z‘ # maxstr
- $foo = reduce { $a < $b ? $a : $b } 1..10 # min
- $foo = reduce { $a lt $b ? $a : $b } ‘aa‘..‘zz‘ # minstr
- $foo = reduce { $a + $b } 1 .. 10 # sum
- $foo = reduce { $a . $b } @bar # concat
- $foo = reduce { $a || $code->(local $_ = $b) } 0, @bar # any
- $foo = reduce { $a && $code->(local $_ = $b) } 1, @bar # all
- $foo = reduce { $a && !$code->(local $_ = $b) } 1, @bar # none
- $foo = reduce { $a || !$code->(local $_ = $b) } 0, @bar # notall
- # Note that these implementations do not fully short-circuit
If your algorithm requires that reduce
produce an identity value, then make sure that you always
pass that identity value as the first argument to prevent undef
being
returned
- $foo = reduce { $a + $b } 0, @values; # sum with 0 identity value
The remaining list-reduction functions are all specialisations of this generic idea.
any
- my $bool = any { BLOCK } @list;
Since version 1.33.
Similar to grep
in
that it evaluates BLOCK
setting $_
to
each element of @list
in turn. any
returns
true if any element makes the BLOCK
return a true value. If BLOCK
never
returns true or @list
was empty then it returns false.
Many cases of using grep
in
a conditional can be written using any
instead, as it can short-circuit after the first true
result.
all
- my $bool = all { BLOCK } @list;
Since version 1.33.
Similar to any, except that it requires all elements of the @list
to
make the BLOCK
return true. If any element returns false, then it returns false. If the BLOCK
never
returns false or the @list
was empty then it returns true.
none
notall
Since version 1.33.
Similar to any and all, but with the return sense
inverted. 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
- my $val = first { BLOCK } @list;
Similar to grep
in
that it evaluates BLOCK
setting $_
to
each element of @list
in turn. first
returns
the first element where the result from BLOCK
is a true value. If BLOCK
never
returns true or @list
was empty then undef
is
returned.
- $foo = first { defined($_) } @list # first defined value in @list
- $foo = first { $_ > $value } @list # first value in @list which
- # is greater than $value
max
- my $num = max @list;
Returns the entry in the list with the highest numerical value. If the list is empty then undef
is
returned.
- $foo = max 1..10 # 10
- $foo = max 3,9,12 # 12
- $foo = max @bar, @baz # whatever
maxstr
- my $str = maxstr @list;
Similar to max, but treats all the entries in the list as strings and returns the highest string as defined by the gt
operator.
If the list is empty then undef
is
returned.
- $foo = maxstr ‘A‘..‘Z‘ # ‘Z‘
- $foo = maxstr "hello","world" # "world"
- $foo = maxstr @bar, @baz # whatever
min
- my $num = min @list;
Similar to max but returns the entry in the list with the lowest numerical value. If the list is empty then undef
is
returned.
- $foo = min 1..10 # 1
- $foo = min 3,9,12 # 3
- $foo = min @bar, @baz # whatever
minstr
- my $str = minstr @list;
Similar to min, but treats all the entries in the list as strings and returns the lowest string as defined by the lt
operator.
If the list is empty then undef
is
returned.
- $foo = minstr ‘A‘..‘Z‘ # ‘A‘
- $foo = minstr "hello","world" # "hello"
- $foo = minstr @bar, @baz # whatever
product
- my $num = product @list;
Since version 1.35.
Returns the numerical product of all the elements in @list
. If @list
is
empty then 1
is returned.
- $foo = product 1..10 # 3628800
- $foo = product 3,9,12 # 324
sum
- my $num_or_undef = sum @list;
Returns the numerical sum of all the elements in @list
. For backwards compatibility, if @list
is
empty then undef
is
returned.
- $foo = sum 1..10 # 55
- $foo = sum 3,9,12 # 24
- $foo = sum @bar, @baz # whatever
sum0
- my $num = sum0 @list;
Since version 1.26.
Similar to sum, except this returns 0 when given an empty list, rather than undef
.
KEY/VALUE PAIR LIST FUNCTIONS
The following set of functions, all inspired by List::Pairwise, consume an even-sized list of pairs. The pairs may be key/value associations from a hash,
or just a list of values. The functions will all preserve the original ordering of the pairs, and will not be confused by multiple pairs having the same "key" value - nor even do they require that the first of each pair be a plain string.
pairgrep
Since version 1.29.
Similar to perl‘s grep
keyword,
but interprets the given list as an even-sized list of pairs. It invokes the BLOCK
multiple
times, in scalar context, with $a
and $b
set
to successive pairs of values from the @kvlist
.
Returns an even-sized list of those pairs for which the BLOCK
returned true in list context,
or the count of the number of pairs in scalar context. (Note, therefore, in scalar context that it returns a number half the size of the count of items it would have returned in list context).
- @subset = pairgrep { $a =~ m/^[[:upper:]]+$/ } @kvlist
As with grep
aliasing $_
to
list elements, pairgrep
aliases $a
and $b
to
elements of the given list. Any modifications of it by the code block will be visible to the caller.
pairfirst
Since version 1.30.
Similar to the first function, but interprets the given list as an even-sized list of pairs. It invokes the BLOCK
multiple
times, in scalar context, with $a
and $b
set
to successive pairs of values from the @kvlist
.
Returns the first pair of values from the list for which the BLOCK
returned true in list context,
or an empty list of no such pair was found. In scalar context it returns a simple boolean value, rather than either the key or the value found.
- ( $key, $value ) = pairfirst { $a =~ m/^[[:upper:]]+$/ } @kvlist
As with grep
aliasing $_
to
list elements, pairfirst
aliases $a
and $b
to
elements of the given list. Any modifications of it by the code block will be visible to the caller.
pairmap
Since version 1.29.
Similar to perl‘s map
keyword,
but interprets the given list as an even-sized list of pairs. It invokes the BLOCK
multiple
times, in list context, with $a
and $b
set
to successive pairs of values from the @kvlist
.
Returns the concatenation of all the values returned by the BLOCK
in list context, or the count
of the number of items that would have been returned in scalar context.
- @result = pairmap { "The key $a has value $b" } @kvlist
As with map
aliasing $_
to
list elements, pairmap
aliases $a
and $b
to
elements of the given list. Any modifications of it by the code block will be visible to the caller.
See KNOWN BUGS for a known-bug with pairmap
,
and a workaround.
pairs
- my @pairs = pairs @kvlist;
Since version 1.29.
A convenient shortcut to operating on even-sized lists of pairs, this function returns a list of ARRAY references, each containing two items from the given list. It is a more efficient version of
- @pairs = pairmap { [ $a, $b ] } @kvlist
It is most convenient to use in a foreach
loop, for example:
Since version 1.39
these ARRAY references are blessed objects, recognising the two methods key
and value
.
The following code is equivalent:
pairkeys
- my @keys = pairkeys @kvlist;
Since version 1.29.
A convenient shortcut to operating on even-sized lists of pairs, this function returns a list of the the first values of each of the pairs in the given list. It is a more efficient version of
- @keys = pairmap { $a } @kvlist
pairvalues
- my @values = pairvalues @kvlist;
Since version 1.29.
A convenient shortcut to operating on even-sized lists of pairs, this function returns a list of the the second values of each of the pairs in the given list. It is a more efficient version of
- @values = pairmap { $b } @kvlist
OTHER FUNCTIONS
shuffle
- my @values = shuffle @values;
Returns the values of the input in a random order
- @cards = shuffle 0..51 # 0..51 in a random order
KNOWN BUGS
RT #95409
https://rt.cpan.org/Ticket/Display.html?id=95409
If the block of code given to pairmap contains lexical variables that are captured by a returned closure, and the closure is executed after the block
has been re-used for the next iteration, these lexicals will not see the correct values. For example:
Will incorrectly print
- three is 3
- three is 3
- three is 3
This is due to the performance optimisation of using MULTICALL
for the code block, which means
that fresh SVs do not get allocated for each call to the block. Instead, the same SV is re-assigned for each iteration, and all the closures will share the value seen on the final iteration.
To work around this bug, surround the code with a second set of braces. This creates an inner block that defeats theMULTICALL
logic,
and does get fresh SVs allocated each time:
This bug only affects closures that are generated by the block but used afterwards. Lexical variables that are only used during the lifetime of the block‘s execution will take their individual values for each invocation, as normal.
SUGGESTED ADDITIONS
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
SEE ALSO
COPYRIGHT
Copyright (c) 1997-2007 Graham Barr <[email protected]>. 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, <[email protected]>.