R.Mckemey
R.Mckemey

Reputation: 355

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

I need to make a string of n 1's in tcl where n is some variable, how can I do this nicely?

At the moment I'm doing this but there must be a better way.

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

In python I'd write "1"*n.

Upvotes: 0

Views: 605

Answers (2)

toxic_boi_8041
toxic_boi_8041

Reputation: 1482

Solution 1:[Simple Solution]

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

Solution 2:

You have to append "one" inside string n number of times, where n is number of ones you want in string.

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

Output,

puts $output_str ;#Gives output 1111111111

Upvotes: 3

Jackson
Jackson

Reputation: 5657

There is a built in string command to do this:

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

Upvotes: 3

Related Questions