Search code examples
tclconcatenationnewlinestring-concatenation

concat variables in new lines with TCL


I'd like to concat some string-based variables and I want each of them in a new line. However, I can't find the correct way to write the code. This is what I tried:

set mensaje "Error: esta atenuacion: $att dB\n"

gset listaMensajes  [concat [gget listaMensajes] "$mensaje"]

set mensaje "Error: esta atenuacion: $att dB\n"

gset listaMensajes  [concat [gget listaMensajes] "$mensaje"]

puts "[gget listaMensajes]" 

The variable $att takes different values in the different parts of the code.

I also tried:

gset listaMensajes  [concat [gget listaMensajes] "\n" "$mensaje"] 

but none of them work; I get the messages in the same line


Solution

  • The concat command strips whitespace between its arguments, so it isn't much use here. What you probably want is the join command. Since you are using non-standard commands, I can't recreate your case, but say that foo is a command that returns a list of strings:

    join [foo] \n
    

    returns a string consisting of the strings returned by foo, with a newline chaaracter joining each string pair.

    This of course also works if you have a list of strings in a variable called bar:

    join $bar \n
    

    Or if you build a list in a command substitution:

    join [list "foo bar" "baz qux"] \n
    

    Documentation: concat, join