Re: Parse a Tcl variable name inside a string
- From: "tom.rmadilo" <tom.rmadilo@xxxxxxxxx>
- Date: Wed, 31 Oct 2007 17:26:15 -0000
On Oct 31, 8:30 am, "pma...@xxxxxxxxx" <pma...@xxxxxxxxx> wrote:
whoops, sorry for the bug, I meant ${var1} !
Many thanks to you both for your useful and quick help !
Philippe
It isn't clear how you don't know what vars need to be set, yet you
know what they need to be set to. It seems to me that the code
required to fix things up ahead of time would be more rational than
using catch or a regexp. Maybe create a little map of the name of the
string and the variables which must exist. You have already done
something like that.
Here is an example script which handles the basic idea. However, it
still errors out if a variable isn't set. But it provides for a way to
set a default value:
global var_map
#set var_map {
# {myvar {var1 var2} {a_${var1}_b_${var2}} }
# {myvar2 {var2 var3} {r__${var3}__w_${var2}} }
#}
#
#put_var myvar3 {myvar2 myvar} {${myvar2}______iiiii__${myvar}}
set var_map [list]
proc put_var { var required_vars value } {
global var_map
if {[set index [lsearch $var_map [list $var * *]]] > -1 } {
set var_map [lreplace $var_map $index $index [list $var
$required_vars $value]]
} else {
lappend var_map [list $var $required_vars $value]
}
return $index
}
proc list_vars { } {
global var_map
return $var_map
}
proc set_var { var } {
global var_map
#puts $var_map
set required_vars [lsearch -inline $var_map [list $var * *]]
# If the var exists and there are no vars to sub, just return
# the value of the var
if {[llength [lindex $required_vars 1]] == 0} {
if {[info exists ::$var]} {
return [set ::$var]
}
}
# Var doesn't exist, or depends on other vars
foreach __tmp_var [lindex $required_vars 1] {
if {[lsearch $var_map [list $__tmp_var * *]] > -1} {
set $__tmp_var [set_var $__tmp_var]
} else {
upvar \#0 $__tmp_var $__tmp_var
}
}
return [subst [lindex $required_vars 2]]
}
put_var myvar {var1 var2} {a_${var1}_b_${var2}}
put_var myvar2 {var2 var3} {r__${var3}__w_${var2}}
set var1 abc
set var2 efg
set var3 xyz
puts [set_var myvar]
#########
Here is an example run of the script:
% source subst.tcl
a_abc_b_efg
% unset var2
% set_var myvar
can't read "var2": no such variable
% put_var var2 {} "My default for var2"
-1
% set_var myvar
a_abc_b_My default for var2
% put_var myvar3 {myvar2 myvar} {${myvar2}______iiiii__${myvar}}
-1
% set_var myvar3
r__xyz__w_My default for var2______iiiii__a_abc_b_My default for var2
% set var2 newVar2Value
newVar2Value
% set_var myvar3
r__xyz__w_newVar2Value______iiiii__a_abc_b_newVar2Value
%
.
- References:
- Parse a Tcl variable name inside a string
- From: pmaire
- Re: Parse a Tcl variable name inside a string
- From: Bryan Oakley
- Re: Parse a Tcl variable name inside a string
- From: pmaire@xxxxxxxxx
- Parse a Tcl variable name inside a string
- Prev by Date: Re: Expect log_file Issue
- Next by Date: Re: Controlling a windows program
- Previous by thread: Re: Parse a Tcl variable name inside a string
- Next by thread: Swig as starkit anywhere ?
- Index(es):
Relevant Pages
|