user2141046
user2141046

Reputation: 912

how to order a tcl procedure to use variables?

I'm trying to have a very generic function and ordering it to use variables it come across from the outside. I've tried the following (simplified code), but with no use:

set line "found \$find1 at \$find2"
do_search $line

proc do_search {line} {
...
if {[regexp $exp $string match find1 find2} {
     puts "$line"
}

However all I get is: found $find1 at $find2 or, if I don't use the \ before the $find, the value of find before I call the function.

Given that this regexp is part of a while loop while parsing a file, I can't use the values after the proc is called.

Any ideas how can it be done?

Upvotes: 0

Views: 106

Answers (1)

evil otto
evil otto

Reputation: 10582

For your exact style, you want subst:

if {[regexp $exp $string match find1 find2} {
     puts [subst $line]
}

But you might consider using format too:

set fmt "found %s at %s"
do_search $fmt

proc do_search {fmt} {
...
if {[regexp $exp $string match find1 find2} {
     puts [format $fmt $find1 $find2]
}

Upvotes: 2

Related Questions