in reply to compare two arrays

TIMTOWTDI again.

use strict; use warnings; my @arr1 = ( 1, 2, 4, 5, 3 ); my @arr2 = ( 1, 2, 4, 2, 3 ); my @diff = map m{\0} ? 1 : 0, split m{}, join( q{}, @arr1 ) ^ join( q{}, @arr2 ); print qq{@arr1\n@arr2\n@diff\n};

The output.

1 2 4 5 3 1 2 4 2 3 1 1 1 0 1

Cheers,

JohnGG

Update: As pointed out by jwkrahn, this is a stupid solution that only works because all elements are one character. Please ignore or downvote. I'll get my coat.

Replies are listed 'Best First'.
Re^2: compare two arrays
by jwkrahn (Abbot) on Dec 15, 2008 at 11:18 UTC
    $ perl -e' my @arr1 = ( 1, 24, 5, 3 ); my @arr2 = ( 1, 2, 42, 3 ); my @diff = map /\0/ ? 1 : 0, split //, join( "", @arr1 ) ^ join( "", @ +arr2 ); print "@arr1\n@arr2\n@diff\n"; ' 1 24 5 3 1 2 42 3 1 1 1 0 1
     :-(

      Oh dear, not very robust then :-(

      Those weren't vitamin pills, they were stupid pills :-/