Zuckerberg
Zuckerberg

Reputation: 205

Join two strings to form a file name in TCL

I cant get this to work .

set dut xyz
set fname [join {$dut "-CE"} ""]

But This works :

set fname [join {xyz "-CE"} ""]

I want fname as "xyz-CE"

Upvotes: 1

Views: 5184

Answers (4)

screwtop
screwtop

Reputation: 111

While you can frame this as joining a list, there are simpler ways to do it if you treat it as a concatenation. In the spirit of "there's more than one way to do it"...

Simple variable substitution will work for the variable names you've used here (the parsing rules for $ mean that it will stop at the - character):

set dut xyz
set fname "$dut-CE"

You don't even need the quotation marks (in Tcl, everything is a string):

set dut xyz
set fname $dut-CE

You could use braces to delimit the variable name explicitly:

set dut xyz
set fname ${dut}-CE

You could also use an explicit subcommand for retrieving the value of dut (set with one argument returns the value of that variable):

set dut xyz
set fname [set dut]-CE

Provided fname is not set or contains the empty string, you could use append (but note that the append operation would be non-idempotent: subsequent calls would keep appending to the existing value of fname):

set dut xyz
append fname $dut -CE

If you really wanted to do it using join (converting a list into a string), create the list using the list command so that $dut gets evaluated (braces prevent variable substitution). We specify the empty string "" as the join separator, as the default is a space.

set dut xyz
set fname [join [list $dut -CE] ""]

Lastly, while there is a concat command, that returns a list (with the elements separated by spaces, which is not what you want).

Upvotes: 0

abcdefgh
abcdefgh

Reputation: 83

try

set dut xyz
eval set fname [join {$dut "-CE"} ""]

it gives

xyz-CE

Upvotes: 0

Downey_HUff
Downey_HUff

Reputation: 191

AS glenn said braces does not support variable substitution so group it with double quotes like this

set dut xyz
set fname [join "$dut -CE" ""]

Upvotes: 0

andy mango
andy mango

Reputation: 1551

Try the following

set dut xyz
set fname ${dut}-CE

Normal word concatenation doesn't need a join.

Upvotes: 5

Related Questions