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 smc.vnet.net
  • Subject: [mg108870] Solutions provided for "combining elements
  • From: ebaugh at illinois.edu
  • 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

***ORIGINAL QUESTION***
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"


***MATH GROUP SOLUTIONS***

Harvey P. Dale:

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


Bob Hanlon:

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

hello

expr =
  Map[
   ToString,
   {{t, h, i, s}, {i, s}, {a}, {t, e, s, t}, {m, e, s, s, a, g, e}},
   {2}];

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 
word.

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

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


Murray Eisenberg:

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

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]
this
   (* you can't see the trailing blank here, so... *)
   StringLength[trial]
5

(You could also use StringJoin instead of StringInsert.)

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

   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:

   StringTrim@StringJoin[appended]

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

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


Hans Michel:

In[16]:= phrase = 
{{"t","h","i","s"},{"i","s"},{"a"},{"t","e","s","t"},{"m","e","s","s","a
","g","e"}}
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:

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"


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