Concatenating strings with newlines in Tcl

Viewed 284

I am unable to concatenate strings having new lines in tcl. The new lines are ignored. Is there a way to overcome this?

% set pst_data "Power states :-\n"
Power states :-

% set pst_data [concat $pst_data "vcc1\t 1 0 1\n"]
Power states :- vcc1     1 0 1
% set pst_data [concat $pst_data "vcc2\t 2 2 0\n"]
Power states :- vcc1     1 0 1 vcc2      2 2 0
% 

I want the output to come in separate new lines every time

2 Answers

It mentions it in the manual:

This command joins each of its arguments together with spaces after trimming leading and trailing white-space from each of them.

You could try using append instead:

% set pst_data "Power states :-\n"
Power states :-

% append pst_data "vcc1\t 1 0 1\n"
Power states :-
vcc1     1 0 1

% append pst_data "vcc1\t 1 0 1\n"
Power states :-
vcc1     1 0 1
vcc1     1 0 1

%

Of course append is the correct way to accomplish this, but in Tcl you could even do the concat like this:

% set pst_data "Power states :-\n"
Power states :-

% set pst_data "${pst_data}vcc1\t 1 0 1\n"
Power states :-
vcc1     1 0 1
Related