in reply to Re: merging two arrays with OR operation
in thread merging two arrays with OR operation

Much nicer than mine!! (see below, I forgot to put mine)

use Data::Dumper; 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 @result; if (@array1 != @array2) { $equals = 0; } else { $equals = 1; foreach (my $i = 0; $i < @array1; $i++) { if (($array1[$i] eq 0)&&($array2[$i] eq 0)) { push (@result, 0); } else{ push (@result, 1); } } } print Dumper \@result;

Replies are listed 'Best First'.
Re^3: merging two arrays with OR operation
by GrandFather (Saint) on Mar 06, 2019 at 20:18 UTC

    There is nothing wrong with your code except that the if (($array1[$i] eq 0)&&($array2[$i] eq 0)) {... code could be replaced by:

    $result[$i] = $array1[$i] | $array2[$i];

    and your for loop is better written:

    for my $i (0 .. $#array1)

    Note that for and foreach are identical aside from the spelling so personally I never use foreach just because that way I type less.

    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond