Re: Re: String to List
- To: mathgroup at smc.vnet.net
- Subject: [mg77050] Re: [mg76995] Re: String to List
- From: Carl Woll <carlw at wolfram.com>
- Date: Fri, 1 Jun 2007 02:50:33 -0400 (EDT)
- References: <f3jigv$dpo$1@smc.vnet.net> <200705310729.DAA05605@smc.vnet.net>
Szabolcs wrote: >King, Peter R wrote: > > >>I have a string which in general looks something like >> >> a="{x,16,x,1T,x}" >> >>In other words it looks like a list with a mixture of characters and >>numbers and so on. I want to pick out the individual elements of this >>(using the commas as delimiters) >> >>Clearly a[[1]] etc doesn't mean anything >> >>I can do b=ToExpression[a] and I get a list so b[[1]] is x and >>b[[2]]=16 (that is the number sixteen but I can convert this back into = >>a >>string with ToString. The problem is b[[4]] which ToExpression turns >>into T as it thinks I mean 1 x T. >> >>So I would appreciate help with either >> >>i) converting my original string to a list as I would like >> >>Or >> >>Ii) stopping the evaluation of 1T into T so that I can get the string 1T >> >>(The only thought I had was to step through the original string >>character by character telling it to put the characters into different >>elements of a list each time I pass a comma, but this looked a bit >>cumbersome). >> >>Many thanks, >> >>Peter King >> >> >> > >The best solution is to generate the data in a format that is easy to >read for Mathematica, e.g. {"x", "16", "x", "1T", "x"}. > >If you must do the conversion in Mathematica, try this (tested in v5.2): > >In[1]:= a="{x,16,x,1T,x}" >Out[1]= {x,16,x,1T,x} > >In[2]:= StringTake[a, {2,-2}] >Out[2]= x,16,x,1T,x > >In[3]:= StringSplit[%, ","] >Out[3]= {x,16,x,1T,x} > >Szabolcs > >P.S. Is is possible to get Mathematica to print the quotes in standard >form? It is too easy to mistake string for symbols ... > > Use Style (version 6) or StyleForm (version 5.2) with the option ShowStringCharacters->True: Style["ab", ShowStringCharacters->True] Or, you can set this option to True at the Notebook level in the option inspector. Or you can mess with your stylesheet. Carl Woll Wolfram Research