Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TCL : Concatenate a variable and a string

Tags:

Assume we have a variable 'a' set to 12345 :

set a 12345 

Now how do i set a new variable 'b' which contains the value of 'a' and another string say 9876

workaround is something like

set a "12345" set u "9876"  set b $a$u 

but i dont want to specify $u instead i want the direct string to used..

like image 603
user651006 Avatar asked Mar 09 '11 05:03

user651006


People also ask

How do you concatenate strings and variables?

In JavaScript, we can assign strings to a variable and use concatenation to combine the variable to another string. To concatenate a string, you add a plus sign+ between the strings or string variables you want to connect. let myPet = 'seahorse'; console.

How do I append a variable in Tcl?

Append all of the value arguments to the current value of variable varName. If varName doesn't exist, it is given a value equal to the concatenation of all the value arguments. This command provides an efficient way to build up long variables incrementally.

How do I combine two strings in Tcl?

The append command has a different semantics: you pass a variable name as first argument, and a variable list of values as other arguments. I edited your answer changing that: you must use $string1 and $string2 to access the values of the two variables and put the variable name you want to write to.

How do I concatenate lists in Tcl?

The concat command joins each of its arguments together with spaces after first trimming all leading and trailing whitespace, and in the case of a list, the results will be flattened. Although this command will concatenate any arguments provided, we will be focusing on its usage as it applies to the list elements.


2 Answers

You can do:

set b ${a}9876 

or, assuming b is either set to the empty string or not defined:

append b $a 9876 

The call to append is more efficient when $a is long (see append doc).

like image 58
Trey Jackson Avatar answered Sep 17 '22 16:09

Trey Jackson


other option is to use set command. since set a gives value of a we can use it to set value of b like below

set b [set a]9876

like image 20
vaichidrewar Avatar answered Sep 16 '22 16:09

vaichidrewar