Re: Can This be Possible? A bug in Set (=) ?

• To: mathgroup at smc.vnet.net
• Subject: [mg110832] Re: Can This be Possible? A bug in Set (=) ?
• From: John Fultz <jfultz at wolfram.com>
• Date: Thu, 8 Jul 2010 03:14:07 -0400 (EDT)

```The problem isn't in Set, it's in SelectionEvaluate.  SelectionEvaluate doesn't
do what you think it does.  You think it's evaluating the selection immediately.
Instead, it's queuing the selection to be evaluated, and the actual evaluation
occurs after the present evaluation of cell4 is finished.

There are various ways to get the behavior you want, but none particularly easy.
I hesitate to recommend anything specific without having a better idea of what
the big picture is of the thing you're trying to accomplish.

Sincerely,

John Fultz
jfultz at wolfram.com
User Interface Group
Wolfram Research, Inc.

On Wed, 7 Jul 2010 07:43:12 -0400 (EDT), James Stein wrote:
> The definition of Set (=) is straight forward:  "*lhs=rhs*
>
> evaluates  *rhs* and assigns the result to be the value of  *lhs*. From
> then
> on,  *lhs* is replaced by  *rhs* whenever it appears."
>
>
> How then to explain the output of cell 4 below, when cells 1 through 5 are
> evaluated in sequence (but not all at once, e.g. do NOT select all five
> cells and then press shift-return) ?
>
>
> (* cell 1 *)
> foo = 1;
> Print [ foo ]
>
>
> (* cell 2 *)
> foo = 2;
> Print [ foo ]
>
>
> (* cell 3 *)
> ClearAll [ evaluate, foo ];
> evaluate[n_Integer] := Module [ {nb, id, sel},
> Print [ "entering evaluate[", n, "], foo=", foo ];
> nb = EvaluationNotebook [ ];
> id = "cell " <> ToString [ n ];
> (* find desired string and select it: *)
> sel = NotebookFind [ nb, id ];
> If [ sel == \$Failed, Return [ sel ] ];
> (* select entire cell containing string: *)
> SelectionMove [ nb, All, Cell ];
> (* set foo by evaluating selected cell: *)
> SelectionEvaluate [ nb ];
> (* verify[??] that foo has a new value *)
> Print [ "exiting  evaluate[", n, "], foo=", foo ];
> Return [ foo ];
> ];
>
>
> (* cell 4 *)
> foo = 0;
> evaluate [1];
> foo
> evaluate [2];
> foo  (* returns 0 *)
>
>
> (* cell 5 *)
> foo  (* returns 2 *)
>
>
> If you enter the above, not all in a single cell but in five separate
> cells, then evaluate each cell in turn, you will discover that consecutive
> evaluations of 'foo' (the last two) return different values.
>
>
> How can this be?  I was under the impression that Mathematica maintains a
> single symbol table.  In this table will be an entry for "foo", with
> various
> associated definitions, values, syntax information, attributes, etc.  So
> how
> is the following sequence be explained?
>
> Assign foo the value 2.  (penultimate line in cell 4)
>
> Display foo (value is 0)  (last line in cell 4)
>
> Display foo (value is 2)  (last line in cell 5)

```

• Prev by Date: Re: overloading a function name in a package? How to query all names?
• Next by Date: Re: Can This be Possible? A bug in Set (=) ?
• Previous by thread: Can This be Possible? A bug in Set (=) ?
• Next by thread: Re: Can This be Possible? A bug in Set (=) ?