I am using perl grep() to perform a fast lookup on a basic table made up of a array of arrays ie
$table=[[1,2,3],[a,b,d],[A,D,C]];
the current lookup does a
@answers = grep { $_->[0] =~ /a/i && $_->[2] =~ /C/} @$table;
I am trying to think of a way to make the search more flexible I can search for any number of array elements.
I've tried calling a function
%search_elements=(0=>a,2=>C);
@answers = grep { scan($_,\%search_elements) } @$table;
sub scan {
my($row,$refToArgsHash)=@_;
foreach (keys %$refToArgsHash)
{
unless($row->[$_] =~ /$refToArgsHash->{$_}/i)
{
return 0; #fail
}
}
return 1; # ok all match
}
But this is really slow. Does anyone think of a way of making this both flexible and fast ? or suggest a new direction to try. Many Thanks. Wertert
In reply to grep flex
by wertert
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.