Re: labeling problem
- To: mathgroup at smc.vnet.net
- Subject: [mg46460] Re: labeling problem
- From: Bill Rowe <readnewsciv at earthlink.net>
- Date: Thu, 19 Feb 2004 03:02:17 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
On 2/18/04 at 12:36 AM, janos.lobb at yale.edu (Janos) wrote:
>I need to assign names to list elements in a nested list based
>upon their position in the nested list. For example if I have a
>list:
>lst = {{a,b},{c,d},e,f,{g,h,i},j,k,{l.m},{n,o,p},q,{r,s},{{t}}}
>then pos=Position[lst,{_,_,_}] will give me
>{{5}.{9}}
>Now, I am thinking to assign a unique name to the list elements on
>these positions, like p5={g,h,i} and p9={n,o,p}. I did not find
>yet how to create variable names with concatenation auto-magically
>from program. In other programming languages I can do something
>like this:
>var="p"+NumToString[5]
>&var={a,b,c}
The Mathematica equivalent to this is
var = "p"<>ToSring[5]
Evaluate[ToExpression@var]={a,b,c}
And given lst and pos defined as above
MapThread[Set[Evaluate[ToExpression["p"<>ToString@#1]],#2]&,
{Flatten@pos,lst[[Flatten@pos]]}]
will create variables p5 and p9 then assign {g,h,i} to p5 and {n,o,p} to p9
--
To reply via email subtract one hundred and four