MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Re: Dynamically set variable name and value

  • To: mathgroup at smc.vnet.net
  • Subject: [mg108237] Re: [mg108182] Re: Dynamically set variable name and value
  • From: Albert Retey <awnl at gmx-topmail.de>
  • Date: Thu, 11 Mar 2010 06:37:24 -0500 (EST)
  • References: <hn5c8h$7nc$1@smc.vnet.net> <201003100643.BAA02884@smc.vnet.net> <1196C3CC-6B4E-4F42-9E73-40FA7EA577A2@mac.com>

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


  • Prev by Date: Re: gaps in plot of piecewise function
  • Next by Date: ParametricPlot3D with three parameters
  • Previous by thread: Re: Dynamically set variable name and value
  • Next by thread: Re: Dynamically set variable name and value