我如何在 tcl 中制作一串 n 1?

How can I make a string of n 1's in tcl?

我需要在 tcl 中创建一个由 n 个 1 组成的字符串,其中 n 是某个变量,我怎样才能很好地做到这一点?

目前我正在这样做,但一定有更好的方法。

set i 1
set ones 1
while {$i < $n} {
    set ones 1$ones
    incr i}

在python中我会写"1"*n

方案一:[简单方案]

set n 10
puts "[string repeat "1" $n]" ;# To display output on console
set output_str [string repeat "1" $n] ;# To get output in variable

方案二:

你必须 append "one" 在字符串中 n 次,其中 n 是你想要在字符串中出现的次数。

set n 10
set i 0
set ones 1
set output_str ""
while {$i < $n} {
    append output_str $ones
    incr i
}

输出,

puts $output_str ;#Gives output 1111111111

有一个内置的字符串命令可以执行此操作:

% set n 10
10
% string repeat "1" $n
1111111111
%