[NJG] January 15, 2005 Recently I posted the page [A regexp twist]. Not having any feedback I have no idea how much attention it has received. It has, however, suddenly occured to me that neither the title nor the style of the prose were very advertising of its essence. So here is a more direct try. [A regexp twist] provides an extensiont to the functionality of the '''regexp''' command in the form of '''regexp -inline''' ''?other options?'' '''''pattern string script''''' where ''script'' would be executed each time a pattern match occured. (Note that in current Tcl this is illegal so it represents a compatible extension.) The result of the actual match is available to ''script'' in the global match variables ''mVar0'', ''mVar1'', .... ''mVar9''. For Windows users the '''''downloadable zip file''''' contains the source and the extension dll. Those on Linux may either replace the ''.dll'' specific part of the source with whatever is needed for compiling it into an ''.so'' module or replace the function ''Tcl_RegexpObjCmd'' in file ''tclCmdMZ.c'' of their Tcl source distribution with the one in the provided source and recompile Tcl. Lacking time I cannot help those who need a Linux binary. Please note: I have no idea which is the oldest Tcl version number for which this extension works. For Tcl 8.4.x it surely does. ---- [MG] takes a quick shot at this in pure-Tcl on 8.4.9 ... proc regexpScriptPre8.5 {args} { if { [llength $args] < 3 } { error "wrong # args" } set rArgs [lrange $args 0 end-3] set cmd [lindex $args end] eval "foreach x \[regexp -inline $rArgs \[lindex \$args end-2\] \[lindex \$args end-1\]\] \{ \ uplevel 1 \[list [list $cmd]\] \[list \$x\] \ \}" } Or, in 8.5 simplified with {expand} (though untested as I don't have 8.5) proc regexpScript8.5 {args} { if { [llength $args] < 3 } { error "wrong # args" } set rArgs [lrange $args 0 end-3] set cmd [lindex $args end] foreach x [regexp -inline {expand}$rArgs [lindex $args end-2] [lindex $args end-1]] { uplevel 1 [list $cmd] [list $x] } } ---- [MG] Just decided to do a quick test to see what, if anything, the speed difference was... (Desktop) 6 % time {regexpScriptPre8.5 -all . {This is a test string} bleh} 500 635 microseconds per iteration (Desktop) 7 % time {regexpScriptPre8.5 -all . {This is a test string} bleh} 5000 664 microseconds per iteration (Desktop) 8 % time {regexpScriptPre8.5 -all . {This is a test string} bleh} 50000 730 microseconds per iteration (Desktop) 9 % load xregexp.dll Extended regexp handling is in place (Desktop) 10 % time {regexp -inline -all . {This is a test string} bleh} 500 897 microseconds per iteration (Desktop) 11 % time {regexp -inline -all . {This is a test string} bleh} 5000 903 microseconds per iteration (Desktop) 12 % time {regexp -inline -all . {This is a test string} bleh} 50000 1017 microseconds per iteration As you can see, I did the tests using the pre-8.5 version (with eval), rather than the {expand} version. All the tests were done on Tcl 8.4.9, and the Tcl-only version used the "normal" regexp; ie, I only loaded [NJG]'s package after I'd tested the plain-tcl code. Suprisingly, the plain-Tcl version comes out slightly faster. (Oh, the 'bleh' script used there was just: proc bleh {x} { set ::tmp $x; return }