in reply to Need to Create an Array of Differences

Here is another take on the lookup hash, which I use now and then.
Much the same as Aristotle's bit. Although, I thought that I would
throw it in anyway, since I had it ready before reading the replies.
## use hash search to find everyone in @a but not in @b. my %temphash = map { $_ => 1 } @b; my @c = grep { !$temphash{ $_ } } @a;

-xtype

Replies are listed 'Best First'.
Re^2: Need to Create an Array of Differences
by Anonymous Monk on Dec 16, 2004 at 15:46 UTC
    ther's a better way to use grep...
    
    my @difference =  grep /^@a1/, @a2;
    
    #example of sub...
    
    my $difference = diff(\@a1,\@a2);
    
    sub diff {
        my @diff =  grep /[^@{$_[0]}]/, @{$_1};
        return \@diff;
    }
    
    Marco (nemux)
    Bye.
    
      sorry ... is impossible to see in my previous message the -> [] 
      
      then with square brackets visible is :
      
      sub diff_2 {
          my @diff =  grep /[[^@{$_[0]]}]/, @{$_[1]};
          return \@diff;
      }
      
      Marco
      
      
        Better?
        If that worked, it wouldn't be semantically the same as Aristotle's, Zaxo's, or my reply. Nor of the perlfaq or tachyon's (which show a difference of both arrays).

        Did you test your code? It is a nice thought, however, I cannot make that work on 5.6.1, 5.8.1, or 5.8.5
        For a simple array of int's @diff is always undef.
        For an array containing scalars I get "Invalid range in regex" error, which is what I expect.

        -xtype