Re: labeling problem
- To: mathgroup at smc.vnet.net
- Subject: [mg46442] Re: labeling problem
- From: Jens-Peer Kuska <kuska at informatik.uni-leipzig.de>
- Date: Thu, 19 Feb 2004 03:01:49 -0500 (EST)
- Organization: Universitaet Leipzig
- References: <c0utqu$e60$1@smc.vnet.net>
- Reply-to: kuska at informatik.uni-leipzig.de
- Sender: owner-wri-mathgroup at wolfram.com
Hi,
and in Mathematica you can do something like
newSymbol[i_Integer,pre_:"p"]:=ToExpression[pre<>ToString[i]]
how ever this is not very clever to do that because it is very hard
to make a loop over all "pi" symbols or to access all "p1","p2" ...
symbols sequenceial. You should use p[i] instead because it is
much simpler to loop through the new variables..
And with a construction like
lst = {{a, b}, {c, d}, e, f, {g, h, i}, j, k, {l.m}, {n, o, p},
q, {r, s}, {{t}}};
pos = Position[lst, {_, _, _}]
lst[[Sequence @@ #]] & /@ pos
you don't need a For-loop or what ever to get the values
form the positions.
Regards
Jens
Janos wrote:
>
> Before I re-invent the wheel let me ask:
>
> 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}
>
> and I could refer to it afterward as p5 and its value would be {a,b,c}.
> The most handy would be if I could take just the name of the position
> and assign that to it, like:
> For[i=1, i<=Length[pos], i++, NumberToName[pos[[i]]]=lst[[pos[[i]] ]] ]
>
> and I would get:
>
> five={g,h,i} and nine={n,o,p}.
>
> Of course lst[[pos[[i]] ]] where' i' goes from 1 to 2 is somewhat doing
> it but it is a little bit complicated.
>
> I looked the book and the online documentation but could not find a
> function called NumberToName which would do
>
> NumberToName[5]
> five
>
> and vice versa
>
> NameToNumber[five]
> 5
>
> Is there such function or procedure in someone's drawer ?
> If not, then what is the right way to get a unique variable name with
> some clear deterministic nature - like above - from Mathematica ?
>
> Thanks ahead,
> János
>
> ------------------------------------------
> "There was a mighty king in the land of the Huns whose goodness and
> wisdom had no equal."
> Nibelungen-Lied