Each string in @array is always of the same form but with different letters and numbers. If I split the strings by white space, then the important elements are the second ones - ie "y.g", "b.f" and "b.c".@array = ("x y.g z 123", "a b.f c 456", "d b.c w 321")
More specifically, what is important is the first component of these strings, ie "y", "b" and "b"
Now I need to filter @array based on these components mentioned above. The filtering is according to another array
so since $array[0] does not contain a "b" or "q" after splitting and looking at the second element it is removed.@include = ("b","q")
I can do this with a series of splits, and testing the components exist in a test hash
But it is very slow when repeated on many different arrays. Is there are more efficient way to use consecutive splitting and a map function? Maybe somebody can see a more elegant way of solving this problem? Many thanks, Chris.my %testHash = map { $_,1} @include; foreach my $tmp (@array){ my @tokens = split /\s+/,$tmp; my ($test,$rubbish) = split /\./, $tokens[1]; push (@keep, $tmp) if (exists $testHash{$test}); }
In reply to filter an array with consecutive splits by coldy
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |