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


In reply to Help on a keyword topic index by neilwatson

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.