Re: A subst-antial question



Busirane schrieb:
I've used [subst] before, and thought I understood it, but today it
surprised me. I guess subconsciously I was expecting it to work like
the parser, preserving word boundaries, but it doesn't. Here's a toy
example:

% package require comm
4.3
% set pairs {a 1 b 2 c 3}
a 1 b 2 c 3

Now I can create a local array:

% array set foo $pairs
% parray foo
foo(a) = 1
foo(b) = 2
foo(c) = 3

Here's a naive attempt to create a remote array using the same code:

% ::comm::comm send 51395 {array set foo $pairs}
can't read "pairs": no such variable

That's expected, since "pairs" isn't defined in the remote interpreter.
We have to substitute it here, and evaluate the results there. Here's
another naive attempt:

% ::comm::comm send 51395 array set foo $pairs
wrong # args: should be "array set arrayName list"

That's also expected, because of the implicit [concat] behavior of
[comm send]. How 'bout keeping the code in braces (to concat-proof it)
and substituting it?

% ::comm::comm send 51395 [subst {array set foo $pairs}]
wrong # args: should be "array set arrayName list"

Hmm... what's going on?

% subst {array set foo $pairs}
array set foo a 1 b 2 c 3

I was expecting a list of four words, which is what the parser
generates from the same expression. I can think of ways around this,
but am I missing something simple?


The parser does more than simple substitution which subst does. It
groups first, then substitutes things. subst doesn't group, it just
substitutes.

The obvious way around this is probably something like:

subst {array set foo [list $pairs]}

Michael

.