Kriti Kapoor
Kriti Kapoor

Reputation: 157

How do I replace a numeric part of a string by another number in Tcl?

I have a string in a format similar to 1005-abcd and I want to replace the numeric part of this string by another number and make it like 1008-abcd. I can achieve this by using the following -

string map {1005 1008} "1005-abcd"

But I have these numbers in form of variables. For example, $source is 1005 and $new is 1008. When I use the same command like this -

string map {$source $new} "1005-abcd"

Upvotes: 0

Views: 294

Answers (1)

Jerry
Jerry

Reputation: 71578

Braces prevent substitution of the variables. Use another way to give the list and enable substitution. One option:

string map "$source $new" "1005-abcd"

Another (better) option:

string map [list $source $new] "1005-abcd"

Upvotes: 2

Related Questions