5

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.

Harrichael
  • 95
  • 1
  • 9

1 Answers1

8

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.

Hai Vu
  • 37,849
  • 11
  • 66
  • 93
  • 1
    The `eval` will be fine here; the static part looks fine, and the non-static part is a proper list (i.e., it comes out of `split` which does proper list construction). – Donal Fellows Mar 18 '13 at 01:12
  • The {*} construct did it, no errors, thank you!! Where can I find more information on the {*} construct? – Harrichael Mar 18 '13 at 01:21
  • @Harrichael: To see doc about `{*}`, see the man page of `Tcl`. Or you can find it online here: [Argument expansion](http://www.tcl.tk/man/tcl8.5/TclCmd/Tcl.htm#M9 "www.tcl.tk"). – pynexj Mar 18 '13 at 05:40