Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to concisely concatenate strings in Tcl?

Tags:

I can easily concatenate two variables, foo and bar, as follows in Tcl: "${foo}${bar}".

However, if I don't want to put an intermediate result into a variable, how can I easily concatenate the results of calling some proc?

Long hand this would be written:

set foo [myFoo $arg] set bar [myBar $arg] set result "${foo}${bar}" 

Is there some way to create result without introducing the temporary variables foo and bar?

Doing this is incorrect for my purposes:

concat [myFoo $arg] [myBar $arg] 

as it introduces a space between the two results (for list purposes) if one does not exist.

Seems like 'string concat' would be what I want, but it does not appear to be in my version of Tcl interpreter.

string concat [myFoo $arg] [myBar $arg] 

String concat is written about here:

  • http://wiki.tcl.tk/16206
like image 347
WilliamKF Avatar asked Sep 15 '09 23:09

WilliamKF


People also ask

How do I combine strings in TCL?

concat , a built-in command, joins strings together such that if the strings are lists, they are concatenated into one into one list.

What is the most efficient way to concatenate many strings together?

Concatenate Many Strings using the Join As shown above, using the join() method is more efficient when there are many strings. It takes less time for execution.

What is concat command in TCL?

DESCRIPTION. This command treats each argument as a list and concatenates them into a single list. It also eliminates leading and trailing spaces in the arg's and adds a single separator space between arg's. It permits any number of arguments.

What is the correct way to concatenate the strings?

Concatenation is the process of appending one string to the end of another string. You concatenate strings by using the + operator. For string literals and string constants, concatenation occurs at compile time; no run-time concatenation occurs.


1 Answers

You can embed commands within a double-quoted string without the need for a temporary variable:

set result "[myFoo $arg][myBar $arg]" 
like image 83
Bryan Oakley Avatar answered Sep 20 '22 11:09

Bryan Oakley