http://qs321.pair.com?node_id=1230956


in reply to merging two arrays with OR operation

Here is a fun little one with map :) Just for fun, anything above is a 'better' way to do it

use strict; use warnings; my @array1 = (0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1); my @array2 = (0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1); print "@array1\n"; print "@array2\n"; my $i; my @result= map {$_ += $array2[$i++];int($_/2 +.6)} @array1; print "@result\n";

Update

WARNING Lookout though, it messes with the content of array 1
# Here is a version that will not break array 1 use strict; use warnings; my @array1 = (0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1); my @array2 = (0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1); print "@array1\n"; print "@array2\n"; my $i; my @result= map {int( ($_+$array2[$i++])/2 +.6)} @array1; print "@result\n";

Cheers,
R.

Pereant, qui ante nos nostra dixerunt!

Replies are listed 'Best First'.
Re^2: merging two arrays with OR operation
by rsFalse (Chaplain) on Mar 06, 2019 at 12:57 UTC
    Same, but with another one variant inside the map, using shift-right op (Shift Operators):
    my @result = map { $_ + $array2[ $i++ ] + 1 >> 1 } @array1;