Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: merging two arrays with OR operation

by Random_Walk (Prior)
on Mar 06, 2019 at 11:10 UTC ( #1230956=note: print w/replies, xml ) Need Help??


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 (Hermit) 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;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1230956]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2021-04-14 07:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?