Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

eval problem 1

Status
Not open for further replies.

JHami01

Technical User
Oct 17, 2007
2
US
I am rather new to TCL. But I have a project that generates emails. The subect line may contain one to many variable based on a predefined text line.
Here is a simple case of what I'm trying to do.
I call a routine that sets a list of Items based on system conditions.
I get the subject line from my configuration file.
Lets say I get two variable from my first routine
OUT_BOUND_QUE =100
THREAD_NAME =OUT_LAB
SUBJECT = "Connection $THREAD is $OBQ records behind"
I have create two variables to use with a format command.
$STRNG = Connection %s is %s records behind
$VAR {$THREAD_NAME $OUT_BOUND_QUE}

now I run the command format "$STRNG" $VAR. Of course this dose not work. How do I get TCL to expand out the $VAR to it's contents ? Then have it run to create the Subject of my email ?
Can I do this ? How do I do it ?
 
Hello,

if you are using TCL 8.4.x you could do something like

Code:
set OUT_BOUND_QUE 100
set THREAD_NAME   OUT_LAB 

set STRNG "Connection %s is %s records behind"
set VAR   [list $THREAD_NAME $OUT_BOUND_QUE]

set result [eval format [list $STRG] $VAR]
Eval expands all lists members to single arguments.

If you have TCL 8.5:

Code:
...
set result [format $STRG {*}$VAR]
Here you can use the new {*} syntax for expanding list members as arguments.

Cheers!
 
If I understand you correctly (otherwise clear up your questions with pieces of tentative code), for the first question you need something like
Code:
set OUT_BOUND_QUE 100 
set THREAD_NAME OUT_LAB 
set STRNG "Connection %s is %s records behind"
set result [format $STRNG $THREAD_NAME $OUT_BOUND_QUE]
Concerning the second question, I don't think you can substitute in Tcl the content of a variable with the values of variables contained therein. You should use again the [tt]format[/tt] statement.


Franco
: Online tools for structural design
: Magnetic brakes for fun rides
: Air bearing pads
 
Ok I will try adding the actually code in question. This code in this format works. However the quotes around "set cmd" as I understand it may lead to problems. We are running tcl 8.4.
.
keylget THREAD_KEYS OBQ OBQ
keylget THREAD_KEYS PSTAT PSTAT
keylget THREAD_KEYS MSGSOUT MSGSOUT
keylget THREAD_KEYS MSGSIN MSGSIN
keylget THREAD_KEYS LWRITE LWRITE
keylget THREAD_KEYS LREAD LREAD
set indx_list [split $SUBJECT " " ]
foreach name $indx_list {
if { [ regexp -all % $name ] > 0 } {
lappend strng %s
lappend var [format "$%s" [crange $name 1 100] ]
} else {
lappend strng $name
}
}
set st [join $strng " " ]set vr [join $var " " ]
set cmd " format \"$st\" $vr "
set RET [eval $cmd]
return "$RET
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top