in reply to Search and Replace with a Large Dictionary

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.

Replies are listed 'Best First'.
Re^2: Search and Replace with a Large Dictionary
by THuG (Beadle) on Mar 04, 2009 at 20:05 UTC

    Wow, October. I started this process way back in October and am just now getting a chance to try and tackle this problem. Just goes to show how low a priority documentation is.

    Thank you guys for your responses. I like the idea of a hash, I just couldn't visualize how to do it. These examples help.

    Mr Mischief, these are ITM Situations. We just want to generate documentation we can post to a wiki so users can quickly figure out what the cryptic page means. I have no problem walking the XML and generating the rest of the document. The problem was just how to make this single line easier to read.