如何在 Perl 中按元素求和数组? [英] How can I sum arrays element-wise in Perl?
本文介绍了如何在 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?
推荐答案
for Perl 5:
for Perl 5:
use List::MoreUtils 'pairwise';
@sum = pairwise { $a + $b } @arr1, @arr2;
这篇关于如何在 Perl 中按元素求和数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文