一个处理perl 数组的模块
25 Aug 2016
Go backList::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)
常用函数说明示例
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
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
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
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
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
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
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
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
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