Prev: What is the best way to handle file names passed to a TCL script?
Next: How to calculate "elapsed time" and "functions execution time" in log file?
From: Donald Arseneau on 10 Aug 2010 21:44 On Aug 5, 4:59 pm, Ahmad <ahmad.abdulgh...(a)gmail.com> wrote: > Although the regsub way worked with me, I'm curious to know how > "substEnv" function works. It takes a string (your user input) as an argument, and returns the same string after substituting all environment variables. So [substEnv "$HOME/Downloads"] gives "/home/ahmad/Downloads". It works by declaring a local (within substEnv) Tcl variable for every environment variable. There is a half-hearted attempt to prevent misuse of the namespace/global flag "::" in the string, but you can omit the [string map] line to eliminate it. Or it could be expanded to protect without corrupting the string.... proc substEnv { string } { set string [string map { \\ \\\\ : \\: } $string] foreach {ev val} [array get ::env] { set $ev $val } return [subst -nocommand $string] } I didn't make any attempt to make the local variables (string ev val) inaccessible, but that could be done too. I didn't expect they would conflict with regular uppercase environment variable names. If the conflict is really a problem, then the conversion could be done in a small namespace rather than the proc's local scope. Donald Arseneau
From: Donald Arseneau on 10 Aug 2010 21:46 On Aug 5, 4:26 pm, Andreas Leitgeb <a...(a)gamma.logic.tuwien.ac.at> wrote: > There's a whole bunch of little mistakes... ;) One more little mistake: > -) you split for '/', but single quotes aren't special in tcl, so > you split by the literal 3-chars-string: It splits the string at every / and at every '. If there are no apostrophes in the string, it would appear to work. Donald Arseneau.
From: Andreas Leitgeb on 11 Aug 2010 04:59
Donald Arseneau <asnd(a)triumf.ca> wrote: > One more little mistake: > Andreas Leitgeb <a...(a)gamma.logic.tuwien.ac.at> wrote: >> -) you split for '/', but single quotes aren't special in tcl, so >> you split by the literal 3-chars-string: > It splits the string at every / and at every '. > If there are no apostrophes in the string, it would > appear to work. Oups! One of my fmm. I wish reality would change for it, and have split treat the splitchars as RE, but I realize it won't happen ;-) |