0

How it is possible to join the string from a variable with a list of strings? More exactly, I have this:

set Dir "02_E12_SP_el";
set OutputDir [join {$Dir _ forcesElem21.out} ""];

I want OutputDir to be "02_E12_SP_el_forcesElem21.out", but instead I obtain "$Dir_forcesElem21.out"

1
  • It sounds like you're working with filenames. If you are and you're trying to insert directory name separators, use file join which is aware of a number of subtleties that aren't immediately obvious. Commented Apr 8, 2018 at 7:04

1 Answer 1

2

When you wrap a variable in braces {}, it will not be interpreted as a variable.

{$Dir _ forcesElem21.out}

creates a static list.

There are several methods.

The join command concatenates a list of elements together. It is more useful when the list is already built and has the flexibility to specify what to join with (e.g. {, }).

set var 123
set mystr abc
set newstr [join [list $mystr def $var] {}]

Direct concatentation:

set var 123
set mystr abc$var
set mystr ${var}abc

Or the append command:

set var 123
set mystr abc
append mystr $var

References: append, join

Sign up to request clarification or add additional context in comments.

2 Comments

@miculas13, the direct concatenation seems the simplest option: set OutputDir ${Dir}_forcesElem21.out
@miculas13: from Tcl 8.6.2 you can also use string cat $Dir _ forcesElem21.out to get the value you wanted. If your list of elements is itself in a variable, use string cat {*}$elements. See tcl.tk/man/tcl/TclCmd/string.htm#M5.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.