Prev: trying to use InputField as a continually attentive
Next: Finding order of all numbers in a Finite Field
From: Bill Rowe on 4 Apr 2010 07:44 On 4/3/10 at 6:09 AM, ebaugh(a)illinois.edu wrote: >How could I go from: {h,e,l,l,o} to {hello}? In[1]:= StringJoin @@ {"h", "e", "l", "l", "o"} Out[1]= hello which is short hand for: In[2]:= Apply[StringJoin, {"h", "e", "l", "l", "o"}] Out[2]= 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" The sublists can be made into strings as follows: In[3]:= StringJoin @@@ {{"t", "h", "i", "s"}, {"i", "s"}, {"a"}, {"t", "e", "s", "t"}, {"m", "e", "s", "s", "a", "g", "e"}} Out[3]= {this,is,a,test,message} which is short hand for In[4]:= Apply[StringJoin, {{"t", "h", "i", "s"}, {"i", "s"}, {"a"}, {"t", "e", "s", "t"}, {"m", "e", "s", "s", "a", "g", "e"}}, {1}] Out[4]= {this,is,a,test,message} But since no space characters are present, you cannot simply do Apply[StringJoin,... here. First, you need to add the space characters, then join the strings. One way to do that would be: In[5]:= StringDrop[ Apply[StringJoin, StringJoin @@@ (Flatten[{" ", #}] & /@ {{"t", "h", "i", "s"}, {"i", "s"}, {"a"}, {"t", "e", "s", "t"}, {"m", "e", "s", "s", "a", "g", "e"}})], 1] Out[5]= this is a test message Here, I added a space character in front of each sublist. Then I converted the sublists to strings using StringJoin@@@. Next, I combined the strings to a single string using Apply[StringJoin,...]. Finally, I deleted the extraneous leading space with StringDrop[..., 1]
From: Murray Eisenberg on 4 Apr 2010 07:44 I'm going to assume you don't really have symbols (t, h, etc.) but rather one-letter strings ("t", "h", etc.) here. The basic function you want is StringJoin. Thus: 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(a)StringJoin[appended] Obviously, all those steps could be combined, encapsulated into a single function: sentenceFromLetters[lis_] := StringTrim@ StringJoin[StringInsert[#, " ", -1] & /@ Map[StringJoin, lis, 1]] Doubtless there are terser, or at least alternative ways, to do this using pattern-matching for at least some of the steps. If your original list really consisted of lists of symbols rather than lists of one-character strings, you could first convert that to a list of lists of one-character strings by using ToString. On 4/3/2010 7:09 AM, ebaugh(a)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 > > > -- Murray Eisenberg murray(a)math.umass.edu Mathematics & Statistics Dept. Lederle Graduate Research Tower phone 413 549-1020 (H) University of Massachusetts 413 545-2859 (W) 710 North Pleasant Street fax 413 545-1801 Amherst, MA 01003-9305
From: Leonid Shifrin on 4 Apr 2010 07:44 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(a)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 > > > >
From: Bob Hanlon on 4 Apr 2010 07:46 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 Bob Hanlon ---- ebaugh(a)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
From: Simon on 4 Apr 2010 07:46 This is a nice easy one... you're bound to get lots of replies. It's not clear that you start off with characters (in Mathematica, that's strings of length one) - So I'll assume not. Here's my attempt at producing what you want (I've converted the outputs to Raw InputForm so that you can see the quote marks): In[1]:= test1={{t,h,i,s},{i,s},{a},{t,e,s,t},{m,e,s,s,a,g,e}} Out[1]={{t, h, i, s}, {i, s}, {a}, {t, e, s, t}, {m, e, s, s, a, g, e}} In[2]:= test2=Map[ToString,test1,{2}] Out[2]={{"t", "h", "i", "s"}, {"i", "s"}, {"a"}, {"t", "e", "s", "t"}, {"m", "e", "s", "s", "a", "g", "e"}} In[3]:= StringJoin[Riffle[test2," "]] Out[3]="this is a test message" Simon On Apr 3, 6:59 pm, eba...(a)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
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 Prev: trying to use InputField as a continually attentive Next: Finding order of all numbers in a Finite Field |