Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

using a config file

by mmittiga17 (Scribe)
on Jul 30, 2009 at 13:52 UTC ( [id://784614]=perlquestion: print w/replies, xml ) Need Help??

mmittiga17 has asked for the wisdom of the Perl Monks concerning the following question:

Hi All, I am looking for suggestions or to be pointed in the right di +rection on using config files to process lines of perl code. I am fa +miliar with using config modules but I am looking to take it a step d +eeper. What I would like to do is have a perl script look to a conf +ig file in order to decide how to process a record in a text file. Example: I need to map a code from a record to another code. if Trancode0 eq ABC than make Trancode XYZ. Below is how I do it in t +he script but want to avoid having to keep changing the script every +time I need to create a new translation mapping.
if (($TRANCODE0 =~m/(TSP|KSP)/) && ($QUANTITY ne '00000000.00000') && +($QSIGN eq "+") && ($KEYCODE =~m/(10|11|20|60)/)){ $TRAN_CODE = "RCV"; } if (($TRANCODE0 =~m/(TSP|KSP)/) && ($QSIGN eq "-") && ($KEYCODE =~m/(1 +0|11|20|60)/)){ $TRAN_CODE = "DLV"; } if (($TRANCODE0 =~m/(TSP|KSP)/) && ($QUANTITY eq '00000000.00000') && + ($KEYCODE =~m/(10|11|20|60)/)){ $TRAN_CODE = "DLV"; } if (($TRANCODE0 eq "EXP") && ($SECTYPE eq "8")){ $TRAN_CODE = "OPTEXP"; } if (($TRANCODE0 eq "EXP") && ($SECTYPE ne '8')){ $TRAN_CODE = "DLV"; }
Any thoughts or suggestion are deeply appreciated.

Replies are listed 'Best First'.
Re: using a config file
by mzedeler (Pilgrim) on Jul 30, 2009 at 14:34 UTC

    I've had the same challenge and solved it with a tab separated rule file. It could look something like this:

    TRANCODE QUANTITY QSIGN KEYCODE HANDLER /(TSP|KSP)/ /^.*(?<!0000000.00000)$/ + /(10|11|20|60)/ se +t(TRAN_CODE, "RCV") /(TSP|KSP)/ * - /(10|11|20|60)/ se +t(TRAN_CODE, "DLV") ...

    The table is meant as a lookup table where each value (apart from the values in the rightmost field) are lookup keys that are processed left to right like this:

    • /<some regex>/ matches against <some regex>
    • * matches against anything
    • All other values are considered literals and must match exactly (see the plus and minus signs above)

    The different field types have different precedence, so a * will have lower precedence than a regex, which in turn has lower precedence than a literal value.

    The rightmost field is just a value that says what to do when matched. It can be a reference to some handler or just a value to insert somewhere.

    I do have a module to do this kind of lookup, but it is currently not open source. If you really want it, I can ask the owners if they will let me release it.

      Thanks this is a big help. Been racking my brain on this for a while. I would love to try the module if possible. Cheers!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://784614]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-25 08:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found