我怎样才能总结数组元素智能在Perl? [英] How can I sum arrays element-wise in Perl?

查看:189
本文介绍了我怎样才能总结数组元素智能在Perl?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@arr1 = ( 1, 0, 0, 0, 1 );
@arr2 = ( 1, 1, 0, 1, 1 );

我要总结两个阵列的项目获得新的像

I want to sum items of both arrays to get new one like

( 2, 1, 0, 1, 2 );

我可以不用通过数组循环?

Can I do it without looping through arrays?

推荐答案

为Perl 5:

use List::MoreUtils 'pairwise';
@sum = pairwise { $a + $b } @arr1, @arr2;

这篇关于我怎样才能总结数组元素智能在Perl?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆