在 Tcl 中从键盘输入 [英] Input from keyboard in Tcl

查看:37
本文介绍了在 Tcl 中从键盘输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过键盘向 Tcl 脚本提供输入?C 中有没有类似 scanf() 的东西?

How do I give input to a Tcl script through the keyboard? Is there any thing like scanf() in C?

推荐答案

gets 命令可能就是你想要的.

The gets command is probably what you want.

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

scan 命令可用于解析输入类似于 scanf 对 C 的处理方式.它使用以下格式:扫描字符串格式 ?varName varName ...?

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 ...?

因此,将像5只猫"这样的输入解析为单个变量:

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

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

从科林的评论中添加了更多信息.

Added more information from Colin's comment.

这篇关于在 Tcl 中从键盘输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆