in reply to Comparing arrays and returning false if an exception is found
I think that grep is not the right tool for doing what you want. If I am right you want to iterate over all the items in array a and see if they are present in array b. Try the following:
my @SupportedPTs = ('Accounting','Dnd','Essay','FITB','Hotspot','Match +ing','MC','Narrative','SingleAnswer','SelfGradingGeneric','Sketch','S +tatic','TF'); my @ProblemTypes = ('Accounting','Dnd','test'); my %seen = (); foreach (@ProblemTypes) { $seen{$_} = 1; } for (@SupportedPTs){ if (! $seen{$_}){ print "$_ is supported\n"; } else { print "$_ is not supported\n"; } }
Outputs:
Accounting is not supported Dnd is not supported Essay is supported FITB is supported Hotspot is supported Matching is supported MC is supported Narrative is supported SingleAnswer is supported SelfGradingGeneric is supported Sketch is supported Static is supported TF is supported
Update:If you want to use grep (a solution less efficient than the previous), you don't need the temporary hash:
for my $sPT (@SupportedPTs){ if (! grep{/$sPT/} @ProblemTypes){ print "$sPT is supported\n"; } else { print "$sPT is not supported\n"; } }
Hope this helps,
citromatik
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Comparing arrays and returning false if an exception is found
by ysth (Canon) on Apr 17, 2008 at 16:32 UTC |