in reply to Combining arrays with grep/unless?
As others have already pointed out, there's a better way. That said, the reason your code fails is the grep isn't doing any kind of test. You want to actually check whether $hit is among the elements in @group1, not whether it is itself true or not.
use Data::Dumper; my @group1 = ('A','B','C','D','E'); my @group2 = ('F','G','H','I','J'); foreach my $hit (@group2) { push (@group1, $hit) unless grep { $_ eq $hit } @group1; } print Dumper( \@group1 );
Output:
$VAR1 = [ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J' ];
As long as we're pretending hashes don't exist, I'll mention also that List::Util has a better function for testing list membership called first. It's similar to grep, but it will stop searching as soon as it finds a match.
|
|---|