tags:

views:

239

answers:

1

How to give inputs to a tcl script through keyboard. is there any thing like scanf() in C?

+6  A: 

The gets command is probably what you want.

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

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

Thus, to parse an input like "5 cats" to individual variables:

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

Edit: Added more information from Colin's comment.

RHSeeger
But if you want to break the input line that gets gives you into multiple values the way scanf() does, you might want to apply the scan command (http://www.tcl.tk/man/tcl8.5/TclCmd/scan.htm) to its result, eg. scan [gets stdin] "%d:%d" hours minutes
Colin Macleod