MathGroup Archive 2007

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

Search the Archive

Re: String to List

  • To: mathgroup at smc.vnet.net
  • Subject: [mg76973] Re: [mg76959] String to List
  • From: János <janos.lobb at yale.edu>
  • Date: Thu, 31 May 2007 03:17:50 -0400 (EDT)
  • References: <200705300936.FAA13551@smc.vnet.net>

On May 30, 2007, at 5:36 AM, 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

Here is a newbie approach:

In[43]:=
s = StringCases[StringReplace[
     a, "," -> " "],
    StringExpression[
     WordBoundary,
     StringExpression[_,
      StringExpression[
       ShortestMatch[__],
       WordBoundary]]]]
Out[43]=
{"x ", "16", " x", " 1T",
   " x"}

In[47]:=
s[[4]]
Out[47]=
" 1T"

J=E1nos


----------------------------------------------
Trying to argue with a politician is like lifting up the head of a 
corpse.
(S. Lem: His Master Voice)



  • References:
  • Prev by Date: Re: Changing default size of new notebook
  • Next by Date: Re: Re: why won't style sheet appear in Stylesheet
  • Previous by thread: String to List
  • Next by thread: Re: String to List