【问题标题】:How can I extract and compare specific number values from a file with tcl?如何使用 tcl 从文件中提取和比较特定数值?
【发布时间】:2012-07-28 09:27:03
【问题描述】:

我想从 tcl 的文件中读取特定的数值并比较它们。例如:

node4 time: 3.03 PositionX :34.596   PositionY: 200.34
node5 time: 6.02 PositionX :4.50   PositionY: 30.234

我需要读取节点号,例如 node4 和 3.03 以及 200.34 以此类推,然后打印它们或稍后进行比较。我如何在 tcl 中做到这一点?

【问题讨论】:

  • 请通过发布您已应用于该问题的一些properly formatted 代码来改进您的问题。此外,请包含格式正确的预期输出示例,以便人们了解您想要达到的结果。

标签: file file-io compare line tcl


【解决方案1】:

看看scan

例如:(循环执行)

scan $line "node%i time: %f PositionX :%f   PositionY: %f" nodenr time posx posy

然后你可以使用变量nodenrtimeposxposy

对于文件部分,您只需要openreadclose。请参阅此Tcl tutorial。要将整个文件拆分为列表(可与foreach 一起使用),请使用split

【讨论】:

  • 另一个可能的工具是正则表达式,但scan 正是处理这类事情的正确工具。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-19
  • 2021-12-30
  • 1970-01-01
  • 2021-11-03
  • 1970-01-01
相关资源
最近更新 更多