eyepopslikeamosquito has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
|---|