7个perl数组高级操作技巧分享

作者:junjie 时间:2022-08-09 10:38:19 

1、去除一个数组中的重复元素:

使用grep函数代码片段:
代码:


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

使用转换hash代码片段:
代码:


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

2、合并两个array:


push @array1, @array2;

3、快速查找最大值,不知道的程序猿们,这样搞:


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


知道的这样搞:


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


知道的他们还这样搞:


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


字符串比较玩弄于掌中。还有sum:


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

4、列表归并

数字求和,也可以用List::Util中的reduce:


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


与sort类似,reduce也是用code block作为参数,不过运行机制稍微不同。每次迭代,先从参数列表取出前面两个元素,分别设置为别名$a和$b,这样参数列表的长度就会缩短为两个元素。然后reduce把语句块返回的计算结果再压回到参数列表的头部。如此往复,直到最后列表里只剩下一个元素,也就是迭代的计算结果$sum。

好了,可以这样了:


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

5、判断是否有元素匹配

纯粹用Perl实现,找到列表中第一个符合某条件的元素,比找出所有符合条件的要麻烦一些。下面的例子,判断是否有大于1000的元素:


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


注意:如果@list有一亿个元素,而要找的就是1001?grep仍然还会循环一亿次,当然你可以向下面自己控制下:


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


还是那句话,不简单~~~List::Util有现成的东西:


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


在List::MoreUtils模块中,也提供很多的实用函数:


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、一次遍历多个列表

一般我们同时遍历多个业务相关的列表时,往往用数组下标遍历:


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

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

看下面这个,你的感觉是?


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


pairwise只适合两个列表的同步计算,三个后用each_array:


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、数组合并

合并多个数组的操作当然你可以自己写,但终究不如MoreUtils的mesh方便:


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 ...

标签:perl,数组,高级操作
0
投稿

猜你喜欢

  • SQL Server 分页查询通用存储过程(只做分页查询用)

    2024-01-12 20:10:11
  • python 中的 BeautifulSoup 网页使用方法解析

    2022-06-25 19:09:26
  • 最常用的SQL语句

    2024-01-23 20:37:40
  • python调用Delphi写的Dll代码示例

    2023-07-25 09:33:53
  • python实现布隆过滤器及原理解析

    2023-03-11 15:01:20
  • MySql实现翻页查询功能

    2024-01-16 11:32:35
  • Python实现将MongoDB中的数据导入到MySQL

    2024-01-21 04:41:01
  • 用MySQL内建复制功能来优化可用性

    2009-02-13 13:55:00
  • Spring数据库事务的实现机制讲解

    2024-01-19 11:32:10
  • 关于axios如何全局注册浅析

    2024-04-27 16:03:40
  • vue自定义filters过滤器

    2024-04-30 08:45:12
  • Python常用配置文件ini、json、yaml读写总结

    2023-07-23 18:41:29
  • 实例:ASP与ACCESS链接

    2008-11-21 16:10:00
  • 浅谈keras使用中val_acc和acc值不同步的思考

    2023-03-14 18:29:45
  • Python中的线程操作模块(oncurrent)

    2021-08-15 14:08:15
  • python通过apply使用元祖和列表调用函数实例

    2021-02-18 03:18:32
  • Asp.Net MVC3.0如何项目部署到Win7 64位系统

    2024-03-12 19:59:34
  • Vue.js中的图片引用路径的方式

    2024-05-09 15:28:10
  • 简单代码屏蔽超级链接虚线框

    2008-02-03 11:34:00
  • 巧制可全屏拖动的图片

    2008-05-09 19:34:00
  • asp之家 网络编程 m.aspxhome.com