Further to Properties, metadata and Perl celebrities, I took simonm's advice and replaced
the boolean fields with a single field of space-separated keywords.
Given the functions get_eye_shapes to get all
shape names, and get_eye_properties to get
a shape's property hash reference, I've written a new
function to get all shapes that contain a list of keywords:
sub find_eye_shapes {
my @key = map( [ split/\s+OR\s+/ ], @_);
my @ret;
for my $s (get_eye_shapes()) {
my $p = get_eye_properties($s) or next; # no properties
exists($p->{keywords}) or next; # no keywords property
my %h; @h{ split(" ", $p->{keywords}) } = ();
my $found = 1;
for my $k (@key) {
grep(exists($h{$_}), @$k) or $found = 0, last;
}
push(@ret, $s) if $found;
}
return @ret;
}
This function has an implicit AND between each of its
parameters. Some examples:
# Get all shapes with keywords: face AND person AND perlhacker
my @faces = find_eye_shapes('face', 'person', 'perlhacker');
# Get all shapes with keywords: face AND (person OR animal)
my @faces = find_eye_shapes('face', 'person OR animal');
Suggestions for improvements, both to the interface and the code
above are welcome.
- The above interface is simple and hopefully covers the common cases, but is not complete because it does not accept an arbitrary boolean expression with AND, OR, NOT, XOR operators and parens. Huffman encoding suggests you should make easy shortcuts for common cases. Oh all right, I'm too lazy/stupid to implement a complete version without first asking here. :-)
- Given that there is an existing get_eye_shapes function (taking no parameters), should I introduce a new name find_eye_shapes or overload the existing get_eye_shapes function with a version that takes a list of keywords?
- Suggestions for a better name than find_eye_shapes are welcome.
- I was pondering using "any" and "all" (stolen from Perl 6 junction names) in the names or parameters somehow.
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.