Wednesday, July 21, 2010

Finding elements in one array but not other...

@A = qw(a, b, c, d, e);
@B = qw(a, b, e);

@Aonly= ();
%seen = ();

foreach $ele (@B) {
%seen{$ele} = 1;
}

foreach $ele (@A) {
unless ($seen{$ele)) {
###push that element if not seen already.
push (@Aonly, $ele);
}
}

######2nd method##

@seen{@B} = ();

foreach $ele (@A) {
### push that element if it doesn't exist in @B or not seen in @B
push (@Aonly, $lele) unless exists $seen{$ele} ;
}

No comments:

Post a Comment