How can I sum arrays element-wise in Perl?

前端 未结 9 535
孤城傲影
孤城傲影 2020-12-15 07:53

I have two arrays:

@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

<         


        
9条回答
  •  孤街浪徒
    2020-12-15 08:28

    If you're using Perl 6:

    @a = (1 0 0 0 1) <<+>> (1 1 0 1 1)  #NB: the arrays need to be the same size
    

    The Perl 6 Advent Calendar has more examples.

提交回复
热议问题