J.Doe
J.Doe

Reputation: 3

other file with regexp

Im have many tcl scripts and in all the same lots of regexp entries.

regexp one exmpl:

if {[regexp -nocase {outl} $cat]} { set cat "outlook" }

how can insert all my regexp in a file and load this in a proc?

exampl:

proc pub:mapping {nick host handle channel text} {

set cat [lindex [split $text] 1];

#regexp i want hier load the file for regexp
if {[regexp -nocase {outl} $cat]} { set cat "outlook" }

putnow "PRIVMSG $channel :new $cat"
}

Regards

Upvotes: 0

Views: 81

Answers (2)

glenn jackman
glenn jackman

Reputation: 247210

If you're just wanting to expand abbreviations, then you can use string map

proc expand_abbreviations {string} {
    # this is an even-numbered list mapping the abbreviation to the expansion
    set abbreviations {
        outl    outlook
        foo     foobar
        ms      Microsoft
    }
    return [string map $abbreviations $string]
}

This approach will be quite fast. However, if the string already contains "outlook", it will be turned into "outlookook"

Upvotes: 0

Marijan
Marijan

Reputation: 136

If I understand you correctly, you now have a bunch of Tcl scripts with large portions of code being repeated among them (in your case, various regex comparisons). In that case, it makes a lot of sense to extract that code into a separate unit.

This could be, as you suggest, become a sort of a text file where you would list regex expressions and their results in some format and then load them when needed in Tcl scripts. But I feel this would be too complicated and ungainly.

Might I suggest you simply create a regex checking proc and save that into a .tcl file. If you need regex checking in any of your other scripts, you can simply source that file and have the proc available.

From your question I'm not quite sure how you plan on using those regex comparisons, but maybe this example can be of some help:

# This is in regexfilter.tcl
proc regexfilter {text} {
    if {[regexp -nocase {outl} $text]} { return "Outlook" }
    if {[regexp -nocase {exce} $text]} { return "Excel" }
    if {[regexp -nocase {foo} $text]} { return "Bar" }
    # You can have as many options here as you like.
    # In fact, you should consider changing all this into a switch - case
    # The main thing is to have all your filters in one place and
    # no code duplication 
}

#
# This can then be in other Tcl scripts
#

source /path_to_filter_scipt/regexfilter.tcl

proc pub:mapping {nick host handle channel text} {
    set cat [lindex [split $text] 1]
    set cat [regexfilter $cat]
    putnow "PRIVMSG $channel :new $cat"
}

Upvotes: 1

Related Questions