On Fri, 14 Jan 2000, John N S Gill wrote:
> Thanks Rick and Ken for your offerings on splitting lists.
>
> I now understand why eval has to be used. I had been living in hope
> that there was some command that would take a list as an argument and
> return the list split out into arguments. I've now put my brain
> into gear: a TCL command always returns a list, there is no way a
> command can be used to split a list into separate arguments.
I guess I don't understand what you were hoping.
Is it this:
set args [list a1 a2 a3]
myCommand [magic_split_args $args]
where myCommand looks like this:
proc myCommand {var1 var2 var3} { ...
If that's what you originally were looking for then now you know that the
eval command is rebuilding the actual tcl list that is eventually
interpreted as a command, and the internal concat step allows myCommand to
see the separate arguments.
The only other thing is to split the list into separate variables:
set args [list a1 a2 a3]
foreach {v1 v2 v3} $args { break }
myCommand $v1 $v2 $v3
but I don't think you really want this in your case.
> If I understand correctly the eval command works as follows:
> 1. takes its arguments and uses the same algorithm as concat
> to build a command.
> 2. passes that command to the TCL interpreter.
Sounds good enough to me.
> I suppose eval does one other thing between 1 & 2. It takes the list
> that results from the concat command and splits it into separate
> arguments to pass to the interpreter.
Umm, this doesn't sound so good. I don't think there is anything between 1
& 2. The output of concat _is_ a list with some number of elements and it
is that list that is given to the interpreter. The "apparent splitting" is
just the concat action and no other actual splitting is there, IMHO. I'm
sure Ken's descriptions are more accurate, but more detail then I need.
> Because the eval command is done in C code it is able to do this
> magic.
Maybe seeing this will help:
% set args [list "a b" "c d" "e f" "g h"]
{a b} {c d} {e f} {g h}
% set cmd [concat myCommand $args]
myCommand {a b} {c d} {e f} {g h}
% proc myCommand {a b c d} {
foreach var {a b c d} {
puts $var=[set $var]
}
return
}
% eval $cmd
a=a b
b=c d
c=e f
d=g h
You see, I didn't need to do anything between step 1 and step 2. If you're
worried about the fact that I'm executing "eval" to pass my list to the
interpreter and that it's doing some splitting, well, given just one
argument, you could say that eval doesn't actually do anything: (take your
pick)
% set cmd
myCommand {a b} {c d} {e f} {g h}
% puts $cmd
myCommand {a b} {c d} {e f} {g h}
% concat $cmd
myCommand {a b} {c d} {e f} {g h}
% puts [concat $cmd]
myCommand {a b} {c d} {e f} {g h}
% puts [concat [concat [concat [concat $cmd]]]]
myCommand {a b} {c d} {e f} {g h}
...RickM...
---------------------------------------------------------------------------
To unsubscribe from the Visual Tcl mailing list, please send a message
to [EMAIL PROTECTED] with "unsubscribe vtcl [EMAIL PROTECTED]" in the
message body (where [EMAIL PROTECTED] is your e-mail address).