Hi Monks, I have an array
@array = ("x y.g z 123", "a b.f c 456", "d b.c w 321")
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".

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

@include = ("b","q")
so since $array[0] does not contain a "b" or "q" after splitting and looking at the second element it is removed.

I can do this with a series of splits, and testing the components exist in a test hash

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}); }
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.

In reply to filter an array with consecutive splits by coldy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.