For Programmers: Free Programming Magazines  


Home > Archive > Tcl > October 2005 > How to execute TCL command stored in a variable









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How to execute TCL command stored in a variable
dkuo@adexa.com

2005-10-24, 7:00 pm

Hi! How are you all?

I would like to know "How to execute TCL command previous stored in a
variable".

My requirement is that I need to execute a list of TCL command which is
previous store in a list like "{expr 1 + 2} {expr 2 * 6}"

I was sucessfully in echo or reading the individual string in the list,
but don't know how to execute in the command line one by one.

I try to do the following at the command and got an error.
>lappend a [lindex $list 0]
>$a


then I will get error like "invalid command name "{expr 1 + 2}""


Looking forward for any kind input or suggestion

Sincerely,

David

suchenwi

2005-10-24, 7:00 pm

Here's an iteractive tclsh session:
% set a {expr 1+2}
expr 1+2
% $a
invalid command name "expr 1+2"

....because $a is taken as one "word".

25 % eval $a
3
17 % set b {{expr 1+2} {expr 3+4}}
{expr 1+2} {expr 3+4}

% eval $b
invalid command name "expr 1+2"

.... because the first list element is not a command name in itself. To
iterate over a list of commands:

% set result {}; foreach cmd $b {lappend result [eval $cmd]}
% set result
3 7

graeme.pietersz@gmail.com

2005-10-24, 7:00 pm


dkuo@adexa.com wrote:

> I try to do the following at the command and got an error.
> lappend a [lindex $list 0]
> $a
>
> then I will get error like "invalid command name "{expr 1 + 2}""


The problem is that the whole of {expr 1 + 2} is grouped together and
therefore TCL sees {expr 1 + 2} as the command name rather than just
expr so

[[lindex $list 0]] returns the error above

however

[lindex $list 0 0] [lrange [lindex $list 0] 1 end]

returns 3 as expected.

However, while this works for expr which needs a single argument, it
will get very messy for other commands.

A better solution will be to use the eval command.

[eval [lindex $list 0]]

Cameron Laird

2005-10-24, 7:00 pm

In article <1130158972.469409.52780@z14g2000cwz.googlegroups.com>,
<dkuo@adexa.com> wrote:
>Hi! How are you all?
>
>I would like to know "How to execute TCL command previous stored in a
>variable".
>
>My requirement is that I need to execute a list of TCL command which is
>previous store in a list like "{expr 1 + 2} {expr 2 * 6}"
>
>I was sucessfully in echo or reading the individual string in the list,
>but don't know how to execute in the command line one by one.
>
>I try to do the following at the command and got an error.
>
>then I will get error like "invalid command name "{expr 1 + 2}""

Ralf Fassel

2005-10-24, 9:57 pm

* dkuo@adexa.com
| My requirement is that I need to execute a list of TCL command which
| is previous store in a list like "{expr 1 + 2} {expr 2 * 6}"

set commands [list]
lappend commands {expr 1 + 2}
lappend commands {expr 2 * 6}
lappend commands {expr {3 * 9}}

foreach cmd $commands {
puts "$cmd => [eval $cmd]"
}

Note that if you can't control how the expr's are stored in the list
(e.g. arbitrary user input), you most likely should not blindly use
'foreach', since the user input might not be a well-formed TCL list.

Also note that it is considered good style to brace the expressions
(like in the {3*9} example). See http://wiki.tcl.tk/583 for more
details on 'expr'.

HTH
R'
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com