Re: associate a parameter name with a string
- To: mathgroup at smc.vnet.net
- Subject: [mg75425] Re: associate a parameter name with a string
- From: David Bailey <dave at Remove_Thisdbailey.co.uk>
- Date: Sun, 29 Apr 2007 03:15:30 -0400 (EDT)
- References: <f0v5ve$8td$1@smc.vnet.net>
luhao wrote:
> Dear all,
> I have groups of files and I'd like to call a function with each file and assign the result
> to a parameter with similar name as the file. How can I do that?
>
> Here are what I tried.
>
> In[30]:=
> Map[ToExpression[StringReplace[#,"_26.txt"->"phe"]]&,FileNames["fd*26.txt"]]
> Out[30]=
> {fd01001phe,fd01002phe,fd01003phe,fd01004phe,fd01005phe,fd01phe}
>
> In[31]:=
> FileNames["fd*26.txt"]
> Out[31]=
> {fd01001_26.txt,fd01002_26.txt,fd01003_26.txt,fd01004_26.txt,fd01005_26.txt,\
> fd01_26.txt}
>
> In[32]:=
> MapThread[#1=f[#2]&,{%30,%31}]
> Set::write : Tag Slot in #1 is Protected.
> Out[32]=
> {f[fd01001_26.txt],f[fd01002_26.txt],f[fd01003_26.txt],f[fd01004_26.txt],
> f[fd01005_26.txt],f[fd01_26.txt]}
>
> Any suggestions?
>
> Thanks,
> Hao
>
You have already created a list of *strings* corresponding to the files
in question:
strlist={fd01001phe,fd01002phe,fd01003phe,fd01004phe,fd01005phe,fd01phe}
You need to convert this list to symbols - after first removing any
existing values that those symbols might have - and then assign them to
a list of values - computed as required. Let us call that list vals:
Clear[strlist];
Evaluate[ToExpression[strlist]]=vals
The crucial thing to understand is that a character string is not the
same thing as a symbol - even though it usually prints the same way. Use
FullForm to see the difference.
David Bailey
http://www.dbaileyconsultancy.co.uk