Keyboard input in Tcl

How can I enter input into a Tcl script through the keyboard? Is there something like scanf()in C?

+3
source share
2 answers

Perhaps the gets command is what you want.

set data [gets stdin]
# or
set numchars [gets stdin data]

The scan command can be used to parse input, similar to how scanf works with C. It uses the format: scan string format? varName varName ...?

Thus, for parsing input data, such as “5 cats,” for individual variables:

set data [gets stdin]
scan $data "%d %s" myint mystring

Edit: Added more information from Colin's comment.

+18
source
puts -nonewline "Enter your name: "
flush stdout
set name [gets stdin]

puts "Hello $name"
+2
source

Source: https://habr.com/ru/post/1724681/


All Articles