       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:= List@
ToExpression[StringJoin @@ Map[ToString, {h, e, l, l, o}]]

Out= {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:= convertToString[{h, e, l, l, o}]

Out= "hello"

In:= convertToString[{{t, h, i, s}, {i, s}, {a}, {t, e, s, t}, {m,
e, s, s, a, g, e}}]

Out= "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
>
>
>
>

```

• Prev by Date: Re: Combining elements of a list
• Next by Date: Re: Combining elements of a list
• Previous by thread: Re: Combining elements of a list
• Next by thread: Re: Combining elements of a list