I hope you have the power to reconsider the approach. The first thing you might want to tell us is what sort of thing you want Tivoli EMS to do for you. Are you just wanting to document what the statements mean by translating the filters?

A recursive-descent parser might be overkill here, but don't discount it completely yet. A naive left-to-right token match and replacement may actually work here, though. The language used is pretty simple.

The left-to-right replacements would be simple with a hash. You could use a different top-level hash for each attribute group or you could use a hash of hashes. Two thousand entries is not that large. You could use YAML or XML for the specification if you didn't want to make constant hashes on disk. Just grab a token, see if it exists as a hash key, and replace it with its value. The code for this is simple and often seen (more often than it should be, as many times as the wheel has bene reinvented actually). The below is a simplified place to start, but you probably want to populate the full data structure in some other way for clarity.

use strict; use warnings; my $string = q(*IF *VALUE ManagedSystem.Product *EQ NT *AND *VALUE Man +agedSystem.Status *EQ '*OFFLINE'); my %dict = ( 'ManagedSystem' => { 'Product' => 'Product Code', 'Status' => 'Status', }, '*IF' => 'If', '*VALUE' => 'the value of', '*EQ' => 'is equal to', '*AND' => 'and', q('*OFFLINE') => q('OFFLINE'), ); my @parts = split /\s+/, $string; foreach my $p ( @parts ) { if ( $p =~ m/\./ ) { my ( $attr_grp, $attr ) = split /\./, $p; $p = $dict{ $attr_grp }{ $attr } if exists $dict{ $attr_grp }{ $at +tr }; } else { $p = $dict{ $p } if exists $dict{ $p }; } print $p . ' '; } print "\n";

If using a database, just make a table in the DB for each attribute group. Then make a char column for the attribute and make an index on it. Make another column for the replacement text. Then something simple like "select replacement from ManagedSystem where attribute = 'Product';" will get you the right replacement text. DBI, Rose::DBI, Class:DBI, or DBIx::Class would each be able to help you with that in its own way.


In reply to Re: Search and Replace with a Large Dictionary by mr_mischief
in thread Search and Replace with a Large Dictionary by THuG

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.