neilwatson has asked for the wisdom of the Perl Monks concerning the following question:
Greetings. I came up with this today to be able to retrieve a topic based on keyword. There will not be more more than a few hundred topics, each with a keyword or two. Can anyone suggest a better way?
#!/usr/bin/perl use strict; use warnings; use feature 'say'; use Data::Dumper; # TODO turn into yml as an external file. my @index = ( { keywords => [ qw/ emacs cfengine_el / ], topic => "Cfengine_el is an EMACS plugin to edit your CFEngin +e ". "code: ". "https://github.com/cfengine/core/blob/master/contrib/cfengin +e.el", }, { keywords => [ 'best practices' ], topic => "CFEngine best practices: ". "http://evolvethinking.com/category/cfengine/best-practices/" +. "and https://github.com/atsaloli/cf-health-check", }, ); my %topic; my %keyword; my $i = 0; # Build a fast index for keyword searches for my $next_topic ( @index ) { # Store topic in index. $topic{$i} = $next_topic->{topic}; for my $next_keyword ( @{ $next_topic->{keywords} } ) { # Store keyworkd in index. $keyword{$next_keyword} = $i; } $i++; } say Dumper( \%topic ); say Dumper( \%keyword ); say 'topic for keyword "best practices" is ' . $topic{ $keyword{ 'best practices' } };
Running it:
$VAR1 = { '0' => 'Cfengine_el is an EMACS plugin to edit your CFEngine + code: https://github.com/cfengine/core/blob/master/contrib/cfengine. +el', '1' => 'CFEngine best practices: http://evolvethinking.com/c +ategory/cfengine/best-practices/and https://github.com/atsaloli/cf-he +alth-check' }; $VAR1 = { 'emacs' => 0, 'cfengine_el' => 0, 'best practices' => 1 }; topic for keyword "best practices" is CFEngine best practices: http:// +evolvethinking.com/category/cfengine/best-practices/and https://githu +b.com/atsaloli/cf-health-check
Neil Watson
watson-wilson.ca
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Help on a keyword topic index
by RichardK (Parson) on Sep 17, 2015 at 17:22 UTC |