Anonymous Monk:

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.


In reply to Re: merging two arrays with OR operation by roboticus
in thread merging two arrays with OR operation by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.