perl Module List::Util   

perl Module List::Util

一个处理perl 数组的模块

25 Aug 2016

Go back perl:List::Util

List::Util 在metacpan上获得了117个赞,是核心模块无需安装,集成了大量处理列表(数组)的函数,这是perl内置模块无需安装。这些函数都是经过 C 优化的,所以执行速度很快,不要重复造轮子。

#调用模块
use List::Util qw(
  reduce any all none notall first
 
  max maxstr min minstr product sum sum0
 
  pairs pairkeys pairvalues pairfirst pairgrep pairmap
 
  shuffle
);
#或者这个模块中的函数全部引用List::Util qw(:all)

常用函数说明示例

1.first 取出列表中第一个满足条件的元素.

use List::Util qw(
  reduce any all none notall first
 
  max maxstr min minstr product sum sum0
 
  pairs pairkeys pairvalues pairfirst pairgrep pairmap
 
  shuffle
);

my @array=(7,6,1,15,7,9,18);
my $re=first {$_>7} @array;
print $re,"\n"; #15 
## 15

2.sum 求列表中所有元素的和,列表为空,返回undef

3.sum0 求列表中所有元素的和,列表为空,返回0

use List::Util qw(
  reduce any all none notall first
 
  max maxstr min minstr product sum sum0
 
  pairs pairkeys pairvalues pairfirst pairgrep pairmap
 
  shuffle
);
my @array=(7,6,1,15,7,9,18);
my $sum=sum(@array);
print $sum,"\n";
## 63

4.product 求列表中元素的乘积,列表为空返回1.

use List::Util qw(
  reduce any all none notall first
 
  max maxstr min minstr product sum sum0
 
  pairs pairkeys pairvalues pairfirst pairgrep pairmap
 
  shuffle
);
my @array=(1,4,9);
my $re=product(@array);
print $re,"\n";
## 36

5.max 取列表中数值最大者,列表为空返回undef

6.min 取列表中数值最小者,列表为空返回undef

7.maxstr 取字符串列表中的较大者

8.minstr 取字符串列表中的较小者

use List::Util qw(
  reduce any all none notall first
 
  max maxstr min minstr product sum sum0
 
  pairs pairkeys pairvalues pairfirst pairgrep pairmap
 
  shuffle
);
$foo = minstr 'A'..'Z';          # 'A' 
print "$foo\t";
$foo = minstr "hello","world";   # "hello" 
print "$foo\t";
$foo = maxstr 'A'..'Z' ;         # 'Z' 
print "$foo\t";
$foo = maxstr "hello","world" ;  # "world"
print "$foo\t";
## A    hello   Z   world   

9.any 列表中有一个元素满足条件返回true。

use List::Util qw(
  reduce any all none notall first
 
  max maxstr min minstr product sum sum0
 
  pairs pairkeys pairvalues pairfirst pairgrep pairmap
 
  shuffle
);
my @strings=("hello","abcd","whoami","a");
if( any { length > 5 } @strings ) {  #True 
  # at least one string has more than 10 characters 
  print "I have found at least a string length than 5 "
} ;
## I have found at least a string length than 5

10.all 列表中所有元素满足条件,返回True

use List::Util qw(
  reduce any all none notall first
 
  max maxstr min minstr product sum sum0
 
  pairs pairkeys pairvalues pairfirst pairgrep pairmap
 
  shuffle
);
my @strings=("hello","abcd","whoami","a");
if( any { length > 5 } @strings ) {  #False 
  # at least one string has more than 10 characters 
  print "I have found at least a string length than 5 "
}; #
## I have found at least a string length than 5

11.reduce 利用内置变量$a $b,进行循环处理

use List::Util qw(:all);
$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 

11.shuffle 把列表中的顺序打乱

use List::Util qw(
  reduce any all none notall first
 
  max maxstr min minstr product sum sum0
 
  pairs pairkeys pairvalues pairfirst pairgrep pairmap
 
  shuffle
);
@cards = shuffle 0..51;
print "@cards\n";
## 17 5 21 12 31 35 20 10 8 2 7 32 37 28 30 25 50 27 14 11 18 26 40 36 42 46 45 43 34 13 9 19 39 47 48 38 29 15 3 51 49 1 23 4 0 41 33 24 6 44 16 22

12.uniq 对数字列表去重

这个函数是在List::MoreUtils模块中的

use List::MoreUtils qw(any uniq);

my @aa=(1,2,3,3,3,2,'a','bdd','c','a'); 
my @bb=(2,3,4,2,3,1); 
print join ",",uniq @aa; #1,2,3,a,bdd,c 
## 1,2,3,a,bdd,c

如果上述函数,还不能满足你的话,可以看看List::MoreUtils