Re: Dynamically set variable name and value
- To: mathgroup at smc.vnet.net
- Subject: [mg108194] Re: Dynamically set variable name and value
- From: Nicola Mingotti <n-no-mingotti at g-spam-mail.com>
- Date: Wed, 10 Mar 2010 01:45:36 -0500 (EST)
- References: <hn5c8h$7nc$1@smc.vnet.net>
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