From: Donald Arseneau on
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
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
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 ;-)