国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Perl > 正文

7個perl數(shù)組高級操作技巧分享

2020-10-31 15:05:21
字體:
供稿:網(wǎng)友

1、去除一個數(shù)組中的重復(fù)元素:

使用grep函數(shù)代碼片段:
代碼:

復(fù)制代碼 代碼如下:

my @array = ( 'a', 'b', 'c', 'a', 'd', 1, 2, 5, 1, 5 );
my %count;
my @uniq_times = grep { ++$count{ $_ } < 2; } @array;

使用轉(zhuǎn)換hash代碼片段:
代碼:

復(fù)制代碼 代碼如下:

my @array = ( 'a', 'b', 'c', 'a', 'd', 1, 2, 5, 1, 5 );
my %saw;
@saw{ @array } = ( );
my @uniq_array = sort keys %saw;

2、合并兩個array:

復(fù)制代碼 代碼如下:

push @array1, @array2;

3、快速查找最大值,不知道的程序猿們,這樣搞:

復(fù)制代碼 代碼如下:

my @nums = 0 .. 1000;
my $max = $nums[0];
foreach (@nums) {
$max = $_ if $_ > $max;
}

知道的這樣搞:
復(fù)制代碼 代碼如下:

use List::Util qw(max);
my $max_num = max( 0 .. 1000 );

知道的他們還這樣搞:
復(fù)制代碼 代碼如下:

use List::Util qw(maxstr);
my $max_str = maxstr ( qw( Fido Spot Rover ) );

字符串比較玩弄于掌中。還有sum:
復(fù)制代碼 代碼如下:

use List::Util qw(sum);
my $sum = sum ( 1 .. 1000 );

4、列表歸并

數(shù)字求和,也可以用List::Util中的reduce:

復(fù)制代碼 代碼如下:

use List::Util qw(reduce);
my $sum = reduce { $a + $b } 1 .. 1000;

與sort類似,reduce也是用code block作為參數(shù),不過運行機制稍微不同。每次迭代,先從參數(shù)列表取出前面兩個元素,分別設(shè)置為別名$a和$b,這樣參數(shù)列表的長度就會縮短為兩個元素。然后reduce把語句塊返回的計算結(jié)果再壓回到參數(shù)列表的頭部。如此往復(fù),直到最后列表里只剩下一個元素,也就是迭代的計算結(jié)果$sum。

好了,可以這樣了:

復(fù)制代碼 代碼如下:

my $product = reduce { $a * $b } 1 .. 1000;

5、判斷是否有元素匹配

純粹用Perl實現(xiàn),找到列表中第一個符合某條件的元素,比找出所有符合條件的要麻煩一些。下面的例子,判斷是否有大于1000的元素:

復(fù)制代碼 代碼如下:

my $found_a_match = grep { $_ > 1000 } @list;

注意:如果@list有一億個元素,而要找的就是1001?grep仍然還會循環(huán)一億次,當(dāng)然你可以向下面自己控制下:
復(fù)制代碼 代碼如下:

my $found_a_match = 0;
foreach my $elem (@list) {
$found_a_match = $elem if $elem > 1000;
last if $found_a_match;
}

還是那句話,不簡單~~~List::Util有現(xiàn)成的東西:
復(fù)制代碼 代碼如下:

use List::Util qw(first);
my $found_a_match = fist { $_ > 1000 } @list;

在List::MoreUtils模塊中,也提供很多的實用函數(shù):
復(fù)制代碼 代碼如下:

my $found_a_match = any { $_ > 1000 } @list;
my $all_greater = all { $_ > 1000 } @list;
my $none_greater = none { $_ > 1000 } @list;
my $all_greater = notall { $_ % 2 } @list;

6、一次遍歷多個列表

一般我們同時遍歷多個業(yè)務(wù)相關(guān)的列表時,往往用數(shù)組下標(biāo)遍歷:

復(fù)制代碼 代碼如下:

my @a = ( ... );
my @b = ( ... );
my @c;

foreach my $i ( 0 .. $#list ) {
my ( $a, $b ) = ( $a[$i], $b[$i] );
push @c, $a + $b;
}


看下面這個,你的感覺是?
復(fù)制代碼 代碼如下:

use List::MoreUtils qw(pairwise);
my @c = pairwise { $a + $b } @a, @b;

pairwise只適合兩個列表的同步計算,三個后用each_array:
復(fù)制代碼 代碼如下:

use List::MoreUtils qw(each_array);

my $ea = each_array( @a, @b, @c );

my @d;
while ( my ( $a, $b, $c ) = $ea->() ) {
push @d, $a+$b+$c;
}


雖然還是有點煩,不過也還好了。

7、數(shù)組合并

合并多個數(shù)組的操作當(dāng)然你可以自己寫,但終究不如MoreUtils的mesh方便:

復(fù)制代碼 代碼如下:

use List::MoreUtils qw(mesh);

my @odds = qw/ 1 3 5 7 9/;
my @evens= qw/ 2 4 6 8 0/;

my @nums = mesh @odds, @evens; # print: 1 2 3 4 ...

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 阜新| 美姑县| 新郑市| 延津县| 屏南县| 来凤县| 秀山| 南开区| 桂阳县| 泾源县| 襄樊市| 扶风县| 盱眙县| 永定县| 广饶县| 廉江市| 珠海市| 凤阳县| 黎城县| 杭锦旗| 高尔夫| 腾冲县| 正阳县| 鞍山市| 仁布县| 金寨县| 白沙| 梅河口市| 玉屏| 阿合奇县| 江城| 铜山县| 仪陇县| 舒兰市| 溧阳市| 伽师县| 岳阳县| 大名县| 靖江市| 弋阳县| 社旗县|