From: Thomas Melehan on
Hi: I am have some trouble doing something I think should be relatively direct:

Take two InputFields: in one place a variable name and in another place the value you want to set to that variable name. Have the value assigned to the variable name dynamically update.

Any help?

From: Albert Retey on
Hi,

> Hi: I am have some trouble doing something I think should be
> relatively direct:
>
> Take two InputFields: in one place a variable name and in another
> place the value you want to set to that variable name. Have the
> value assigned to the variable name dynamically update.
>
> Any help?

This should do what you want:

DynamicModule[{varname},
Column[{
InputField[Dynamic[varname], String],
InputField[
Dynamic[
ToExpression[varname],
(Set @@ Append[ToExpression[varname, InputForm, Hold], #]) &
], Number],
Dynamic[varname -> ToExpression[varname]]
}]
]

it is not something that I would recommend, since it will usually fail
when not defined _and_ running in the same context. Also I think you are
you are asking for problems since the user can create as many variable
as he wishes. What is the problem that you try to solve?

hth,

albert

From: Nicola Mingotti on
On 2010-03-09 12:43:13 +0100, Thomas Melehan said:

> Hi: I am have some trouble doing something I think should be relatively direct:
>
> Take two InputFields: in one place a variable name and in another
> place the value you want to set to that variable name. Have the value
> assigned to the variable name dynamically update.
>
> Any help?

You can do like this :

SetAttribute[f , HoldFirst];
f [ var_, value_ ] := var = value

--- testing ---
f [ x , 10] => 10
x => 10
f [ x, 11] => 11
x => 11
----------------

Probably you were trying something like :

g[var_, val_] := var = val

that gives you :

g[x , 10] => 10 ( if x is not bounded to any var untill here)
x => 10
g[x, 11] => Error.


Infact, in the case of function "g" you are defining a regular function.
In the case of function "f" you are defining a "Special Function" .
Look Mathematica Help -> Documentation "Non-Standard Evaluation"
for an explanation.

The best book i've ever seen about these topics is SICP, full
HTML version online here :
http://mitpress.mit.edu/sicp/full-text/book/book.html

bye

nicola




From: Albert Retey on
Hi,

> Albert: That did it, thank you. Thank you also for your words of
> caution - I can see how its use could become problematic. I am
> trying to build an application for non mathematica users and I wanted
> the user to have the ability to create their own variables in the
> same manner one can define a named range in excel.

for such an application I would probably put those user defined
variables in an own Context (namespace), so they won't conflict with
anything else, e.g. like so:

toExpression[varname_String, rest___] :=
ToExpression["UserContext`" <> varname, rest];

toExpression[x_, ___] := x;

DynamicModule[{varname = None},
Column[{
InputField[Dynamic[varname], String],
InputField[Dynamic[
toExpression[
varname], (If[StringQ[varname],
Set @@ Append[toExpression[varname, InputForm, Hold], #]]) &],
Number
],
Dynamic[varname -> toExpression[varname]]
}]
]

Evaluating the following will show that Global`test is not set,
UserContext`test is:

test
UserContext`test

you also have the advantage that it is really easy to get a list of the
variable names that have already been defined:

Names["UserContext`*"]

hth,

albert