Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive

MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Solutions provided for "combining elements

  • To: mathgroup at
  • Subject: [mg108870] Solutions provided for "combining elements
  • From: ebaugh at
  • Date: Mon, 5 Apr 2010 08:00:35 -0400 (EDT)

Thank you so much to everybody who helped my with my question.

I have posted all the solutions that I received in this message so 
that they can be available to the whole group.
The author of each solution is noted.

Jason Ebaugh

How could I go from:

That is the first step of what I am really looking to do. Which is 
to go from:
"this is a test message"


Harvey P. Dale:

,s,s,a,g,e}}," "]]

Bob Hanlon:

StringJoin @@ {"h", "e", "l", "l", "o"}


expr =
   {{t, h, i, s}, {i, s}, {a}, {t, e, s, t}, {m, e, s, s, a, g, e}},

StringJoin @@ Most[Flatten[
   Append[#, " "] & /@ expr]]

this is a test message

Dr. S. S. Tong:

StringJoin[{"h", "e", "l", "l", "o"}]

StringJoin @@ 
 Riffle[{{"t", "h", "i", "s"}, {"i", "s"}, {"a"}, {"t", "e", "s", 
    "t"}, {"m", "e", "s", "s", "a", "g", "e"}}, " "]

Tomas Garza:

In[1]:= StringJoin[{"h", "e", "l", "l", "o"}]

Out[1]= "hello"

Do the same for {"This", " ", "is", " ", "a", " ", "test", " ", 
"message"}, but remember to type the spaces between each 

In[2]:= StringJoin[{"This", " ", "is", " ", "a", " ", "test", " ", 

Out[2]= "This is a test message"

Murray Eisenberg:

 StringJoin[{"t", "h", "i", "s"}]

Next, you have your list of lists:

  lis = {{"t", "h", "i", "s"}, {"i", "s"}, {"a"}, {"t", "e", "s", "t"}};

(I left out the last word.)  So Map the function StringJoin onto 
that at 
level 1:

   Map[StringJoin, lis, 1]
{"this", "is", "a", "test"}

Now you need to append a blank to each word (except the last).  
For a 
single string str StringInsert[str," ",-1] does it, as in:

   trial=StringInsert["this"," ",-1]
   (* you can't see the trailing blank here, so... *)

(You could also use StringJoin instead of StringInsert.)

Now Map that function of appending a blank onto the list of 

   appended = Map[StringInsert[#, " ", -1] &, words]
{this ,is ,a ,test }

Same thing, abbreviated:

   StringInsert[#, " ", -1] & /@ words

Finally, join all the individual (blank-trailed) words into a single 
string and delete the final trailing blank:


Obviously, all those steps could be combined, encapsulated into 
a single 

   sentenceFromLetters[lis_] :=
      StringJoin[StringInsert[#, " ", -1] & /@ Map[StringJoin, lis, 1]

Hans Michel:

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

In[17]:= StringJoin[Flatten[Riffle[phrase," "],Infinity]]
Out[17]= this is a test message

Leonid Shifrin :

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:

convertToString[expr_] :=
  expr //. {
    symbs : {__Symbol} :> StringJoin @@ Map[ToString, 
    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"

Samuel  Thomas Blake

In[14]:= StringJoin @@ ToString /@ Flatten[Riffle[{{t, h, i, s}, {i,
s}, {a}, {t, e, s, t}, {m, e, s, s, a, g, e}}, " "]]
Out[14]= "this is a test message"

  • Prev by Date: Re: Orientation of Arrowheads
  • Next by Date: Re: Speed Up of Calculations on Large Lists
  • Previous by thread: Show[] on 3D graphics, rotation by mouse, how to improve performance
  • Next by thread: Mathematica Programming