Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
257 views
in Technique[技术] by (71.8m points)

Nesting strings in Tcl for puts and join

I want to generate output that looks like this:

foo:
  1
  2
  3

My na?ve attempt would be something like puts "foo: [join {1 2 3} { }]" but of course that doesn't work because { } isn't unescaped and it produces this instead:

foo:
  1
  2
  3

I can't seem to find the right sequence of escapes to nest a string within a string such that join sees the spaces and unescapes the to a newline. Is there a way to do what I want?

question from:https://stackoverflow.com/questions/65645187/nesting-strings-in-tcl-for-puts-and-join

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use double quotes instead of braces:

% puts [join {foo: 1 2 3} "
  "]
foo:
  1
  2
  3

Or instead of trying to build up a single string, use a loop:

puts "foo:"
foreach i {1 2 3} {
    puts "  $i"
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...