Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Why unset doesn't works with a list of elements passed ? Why we need to use eval for this?

5 views
Skip to first unread message

sunny

unread,
May 28, 2011, 9:31:27 AM5/28/11
to
Hi All,

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

George Petasis

unread,
May 28, 2011, 10:09:50 AM5/28/11
to sunny

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

sunny

unread,
May 29, 2011, 10:37:30 AM5/29/11
to

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?

Gerald W. Lester

unread,
May 29, 2011, 1:49:09 PM5/29/11
to

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 |
+------------------------------------------------------------------------+

Donal K. Fellows

unread,
May 29, 2011, 3:55:59 PM5/29/11
to
On May 28, 3:09 pm, George Petasis <petas...@yahoo.gr> wrote:
> What you really need is not eval, but Tcl 8.6 and {*}:
> unset {*}$var_list

That'll also work in 8.5.

Donal.

0 new messages