Sorry for posting out this basic question, but i am having doubt with
usage of eval command i.e. why there is need of eval command.
For instance lets say i have four variables set with some value.
set a1 1
set a2 2
set a3 3
set a4 4
set var_list {a1 a2 a3 a4}
Now when i use $var_list, it will display all the variables list and
llength will return 4.
puts $var_list => a1 a2 a3 a4
llength $var_list will return 4.
Now when i use unset $var_list, it gives error as it takes it list
output as one element while if i use "unset a1 a2 a3 a4" it works.
My question is when i use eval, for it is also a single element, then
how could it have separated the list elements. So does that mean that
if i have a list as
{a b c {d e f}} => four element list will come out as 6 seperate
values after passing it through eval command.
Appreciate if some one can explain the usage of eval.
Thanks
What eval does is very simple: it concatenates all its arguments into a
single string, and passes that to the Tcl interpreter, which parses it,
and executes it.
So, when you do "eval unset $var_list", eval gets two strings:
unset
a1 a2 a3 a4
and produces "unset a1 a2 a3 a4", which executes just fine. Note that
the value of a $var_list does not contain the outer braces you used in
set. If you do "eval unset {a b c {d e f}}", the string that eval makes
is "unset a b c {d e f}".
Using "unset $var_list" does not work, because unset expects that each
argument is a variable name. So, it searches for the variable "a1 a2 a3 a4".
What you really need is not eval, but Tcl 8.6 and {*}:
unset {*}$var_list
will work. Tcl will expand the variable into multiple arguments for you.
George
I have read somewhere that eval command is used to remove one level of
grouping. So in my case eval unset $var_list will remove grouping of
$var_list, thus to unset command separate four arguments are seen.
Now if i do puts $var_list, it appears the one level of grouping is
removed so on output no enclosing braces are seen and only four values
are seen. So if i do it like
unset [puts $var_list]
It gives error, can't unset "": no such variable. Doesn't it should
work?
That is because puts outputs strings not list to stdout.
> So if i do it like
>
> unset [puts $var_list]
>
> It gives error, can't unset "": no such variable. Doesn't it should
> work?
No it should not work -- you need to reread what the puts command *RETURNS*.
--
+------------------------------------------------------------------------+
| Gerald W. Lester, President, KNG Consulting LLC |
| Email: Gerald...@kng-consulting.net |
+------------------------------------------------------------------------+
That'll also work in 8.5.
Donal.