TCL-如何在tcl过程中将字符串解释为多个参数? [英] TCL- How to interpret a string as multiple arguments in tcl procedures?
问题描述
我有一个过程,需要接受一定数量的参数来制作阵列.我有一个csv文件,其中包含每行中参数的信息.使用命令 [split $ line,] ,返回的信息之间带有空格,但过程将其视为一个参数而不是7或8个参数.如何获得csv行,例如:
I have a procedure that needs to accept a set amount of parameters to make arrays. I have a csv file with the information for the parameters on each line. Using the command [split $line ,], returns the information with spaces in between except that procedures treat it as one argument instead of 7 or 8 arguments. How can I get a csv line, such as the following:
day-month-year,34,3,12,5,1,54,21,$big money
被视为多个参数,例如:
to be seen as multiple arguments such as:
date num1 num2 num3 num4 num5 num6 num7 money
或类似以下的变体:
day month year num1 ... num7 big money
split命令返回:
The split command returns:
date num1 num2 num3 num4 num5 num6 num7 {big money}
这很好,除了将其视为单个参数.我的电话看起来像这样:
which is fine except that it is treated as a single argument. My call looks like this:
procName [split $line ,]
谢谢.
推荐答案
如果您使用的是Tcl 8.5或更高版本:
If you are using Tcl version 8.5 or later:
procName {*}[split $line ,]
如果您使用的是Tcl 8.4版,则{*}构造尚不存在,您别无选择,只能执行以下操作:
If you are using Tcl version 8.4, the {*} construct was yet to exist, you have no choice, but to do something like:
eval procName [split $line ,]
请注意,评估可能不安全,特别是如果输入来自未知来源.
Be warned, the eval might be unsafe, especially if the input comes from unknown sources.
{*}构造称为参数扩展.我所知道的是这样的:
The {*} construct is called Argument Expansion. All I know about it is this:
procName {*}{a b c}
与以下相同:
procName a b c
此功能是在Tcl改进建议(TIP)中提出的 293 ,并在 Tcl'er Wiki 中进行了讨论.
This feature was proposed in the Tcl Improvements Proposal (TIP) 293 and was discussed in the Tcl'er Wiki.
这篇关于TCL-如何在tcl过程中将字符串解释为多个参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!