Question

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

or a variation such as the following:

 day month year num1 ... num7 big money

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 ,]

Thank you.

Was it helpful?

Solution

If you are using Tcl version 8.5 or later:

procName {*}[split $line ,]

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.

Update

The {*} construct is called Argument Expansion. All I know about it is this:

procName {*}{a b c}

is the same as:

procName a b c

This feature was proposed in the Tcl Improvements Proposal (TIP) 293 and was discussed in the Tcl'er Wiki.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top