Re: Combining elements of a list
- To: mathgroup at smc.vnet.net
- Subject: [mg108855] Re: Combining elements of a list
- From: Leonid Shifrin <lshifr at gmail.com>
- Date: Sun, 4 Apr 2010 07:45:11 -0400 (EDT)
Hi Jason,
If you want to start with a list of symbols and convert to a symbol, it can
be done for example as follows:
In[1]:= List@
ToExpression[StringJoin @@ Map[ToString, {h, e, l, l, o}]]
Out[1]= {hello}
The following is one way to accomplish what you mentioned as your final
goal:
Clear[convertToString];
convertToString[expr_] :=
expr //. {
symbs : {__Symbol} :> StringJoin @@ Map[ToString, symbs],
words : {__String} :> StringJoin @@ Riffle[words, " "]};
In[4]:= convertToString[{h, e, l, l, o}]
Out[4]= "hello"
In[5]:= convertToString[{{t, h, i, s}, {i, s}, {a}, {t, e, s, t}, {m,
e, s, s, a, g, e}}]
Out[5]= "this is a test message"
There certainly are many other ways to solve this problem.
Working with Symbols is however a fragile practice, since you must be sure
that none of these symbols has a value. I'd recommend to work with Strings
from the beginning.
Regards,
Leonid
On Sat, Apr 3, 2010 at 4:09 AM, <ebaugh at illinois.edu> wrote:
>
> How could I go from:
> {h,e,l,l,o}
> to
> {hello}?
>
> That is the first step of what I am really looking to do. Which is
> to go from:
> {{t,h,i,s},{i,s},{a},{t,e,s,t},{m,e,s,s,a,g,e}}
> to
> "this is a test message"
>
> Thanks all,
> Jason Ebaugh
>
>
>
>