Im trying to filter an array of terms using another array in Perl. I have Perl 5.18.2 on OS X, although the behavior is the same if I am use 5.010. Here is my basic setup:
my @terms = ('alpha','beta test','gamma','delta quadrant','epsilon',
'zeta','eta','theta chi','one iota','kappa');
my @filters = ('beta','gamma','epsilon','iota');
foreach $filter (@filters) {
for my $ind (0 .. $#terms) {
if (grep { /$filter/ } $terms[$ind]) {
splice @terms,$ind,1;
}
}
}
This works to pull strings matching various search queries, but the length of the array does not change. If I write out the resulting array @terms, I get:
[alpha]
[delta quadrant]
[zeta]
[eta]
[theta chi]
[kappa]
[]
[]
[]
[]
As expected, the print scalar(@terms)gets the result 10.
I want to get a resulting array of length 6, with no four empty elements at the end. How to get this result? And why isnt the array shrinking given that the perldocsplice page says: "The array grows or shrinks as needed."
(Im Perl, , : " ...?", , , .)