Re: Symbol name extraction
- To: mathgroup at smc.vnet.net
- Subject: [mg73718] Re: Symbol name extraction
- From: albert <awnl at arcor.de>
- Date: Mon, 26 Feb 2007 06:14:19 -0500 (EST)
- References: <errlbu$83p$1@smc.vnet.net>
Hi, > I have to write a list of parameters to a file, in string format. > In the first shot, I've tried something like this: > > > a = "x"; > b = 0; > c = {1, 2, 3}; > > printSymbol[vars__] :=StringJoin[Map[(SymbolName[#] <> " = " <> > FullForm[#] <>";\n") &, {vars}]]; > > printSymbol[a, b, c] > > > but it fails to recognize symbol names. Any suggestion? What you want to achieve is not completly trivial, since you have to take care that a, b and c are not evaluated before SymbolName sees them. The following is one way to achieve what you need, there are of course several others: In[1]:= ClearAll[printSymbol] In[2]:= SetAttributes[printSymbol,HoldAll] In[3]:= printSymbol[var_Symbol]:=StringJoin[ SymbolName[Unevaluated[var]]," = ",ToString[InputForm[var]],";\n" ]; In[4]:= printSymbol[vars_List]:=StringJoin[ ReleaseHold[Map[printSymbol,Hold[vars],{2}]] ]; In[5]:= printSymbol[{a,b,c}] Out[5]= a = "x"; b = 43; c = {1, 2, 3}; anyway, have you looked at the function Save? I think it does the same thing and writes the resulting string to a file. It is probably more efficient and secure than whatever you can achieve. Just try: Save["mysymbols.m",{a,b,c}] and see whether that fits your needs. hth, albert