Re: Symbols, names, objects: kludge
- To: mathgroup at smc.vnet.net
- Subject: [mg6548] Re: [mg6517] Symbols, names, objects: kludge
- From: David Withoff <withoff>
- Date: Mon, 31 Mar 1997 23:01:47 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
> In an earlier posting, I asked how to define a function > store[varname_String, dataname_String] that causes a variable to be > created whose name is given by the string used as first argument and > which assigns as the value of that new variable the value of the > object whose name is given by the string used as the second argument. > For example, > > x = 99; > store["myx", "x"] > > should mean that an input > > myx > > now produces > > 99 > > as output. > > I found one method that works -- sort of, and sometimes -- but which I > don't fully understand: > > store[varname_String, dataname_String] := > ToExpression[StringJoin[Sequence@@{varname,"=",dataname}]] > > Unfortunately, I need to use such a function store inside a Module, > like this: > > wrapper[shortname_String] := Module[{temp}, > temp = ...... (* value created here *) ; > store["new"<>shortname, "temp"]; > .... > ] > > and now, because temp is a local variable in the Module, the whole > thing breaks down (assigning the SYMBOL temp as the value of the > new... variable). > > A real kludge is: > > wrapperkludged[shortname_String] := > (temp = ...... ; > store["new"<>shortname, "temp"]; > Remove[temp] > ) > > but clearly that is most unpleasant (since, in my actual application, > there are a whole bunch of local variables in the Module that calls > the function store. > > So I'm still seeking a satisfactory solution! > > -- > Murray Eisenberg Internet: murray at math.umass.edu > Mathematics & Statistics Dept. Voice: 413-545-2859 (W) > University of Massachusetts 413-549-1020 (H) > Amherst, MA 01003 Fax: 413-545-1801 My best guess here that you should try Block rather than Module. Block leaves the name of the symbol unchanged, so if you generate a new symbol with that name, it will refer to the local symbol. Module also localizes the name of the symbol, so if you generate a new symbol during evaluation of the Module, that new symbol will refer to something else. Dave Withoff Wolfram Research