in reply to merging two arrays with OR operation
Performing an operation pairwise on a pair of lists is a common enough task that there's a function in the List::MoreUtils package to help you with it: pairwise().
So for your task, you'd simply do it like:
$ cat pm_1230926.pl use strict; use warnings; use List::MoreUtils 'pairwise'; 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); my @arrayResult = pairwise { $a<$b ? $b : $a } @array1, @array2; print "Result: ", join(", ", @arrayResult), "\n"; $ perl pm_1230926.pl Result: 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1
Essentially, you give it a block of code { $a<$b ? $b : $a } and a pair of lists to operate on. The values $a and $b will be the next item in the first and second list, respectively. The result will be the list of values obtained by executing the code block on each pair of items in the list. So in our code block, we check to see if $a is less than $b. If so, we'll return $b otherwise we'll return $a.
Update: choroba suggested to me an even better way:
my @arrayResult = pairwise { $a | $b } @array1, @array2;...roboticus
When your only tool is a hammer, all problems look like your thumb.
|
|---|