Re: associate a parameter name with a string
- To: mathgroup at smc.vnet.net
- Subject: [mg75415] Re: associate a parameter name with a string
- From: Bill Rowe <readnewsciv at sbcglobal.net>
- Date: Sun, 29 Apr 2007 03:10:12 -0400 (EDT)
On 4/28/07 at 5:55 AM, luhaorice at gmail.com (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]} Instead of using "=" in MapThread use Set, i.e., In[1]:= strData={"fd01001_26.txt","fd01002_26.txt","fd01003_26.txt","fd01004_26.t= xt",\ "fd01005_26.txt","fd01_26.txt"}; In[2]:= vars = (ToExpression[StringReplace[#1, "_26.txt" -> "phe"]] & ) /@ strData Out[2]= {fd01001phe,fd01002phe,fd01003phe,fd01004phe,fd01005phe,fd01phe} In[3]:= MapThread[Set[#1,f[#2]]&,{vars,strData}]; In[4]:= vars Out[4]= {f(fd01001_26.txt),f(fd01002_26.txt),f(fd01003_26.txt),f(fd01004_26.txt),f(= \ fd01005_26.txt),f(fd01_26.txt)} I don't recall why it is necessary to use Set[#1,f[#2]]& instead of #1=f[#2]& in this application -- To reply via email subtract one hundred and four