MathGroup Archive 2001

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

Search the Archive

RE: Working with strings

  • To: mathgroup at smc.vnet.net
  • Subject: [mg31948] RE: [mg31940] Working with strings
  • From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.de>
  • Date: Fri, 14 Dec 2001 04:21:09 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

> -----Original Message-----
> From: bghiggins at ucdavis.edu [mailto:bghiggins at ucdavis.edu]
To: mathgroup at smc.vnet.net
> Sent: Thursday, December 13, 2001 7:09 AM
> To: mathgroup at smc.vnet.net
> Subject: [mg31948] [mg31940] Working with strings
> 
> 
> I have a list of string characters that I would like to group in a
> particular way. The list has the following structure:
> 
> {n UpperCase Characters, x, m UpperCase Characters}
> 
> Here is an example:
> 
> myString={"S", "T", "M", "x", "R", "K", "P", "G"}
> 
> Now I would like to form the following groupings:
> 
> {"SxR", "SxK", "SxP", "SxG", "TxR", "TxK", "TxP", "TxG", "MxR", "MxK",
> "MxP", \
> "MxG"}
> 
> A brute force way of doing this is
> 
> subString[n_] := Split[myString, UpperCaseQ[#2] && UpperCaseQ[#1]
> &][[n]];
> 
> Flatten[Outer[StringJoin, Outer[StringJoin, subString[1],
> subString[2]],
>     subString[3]]]
> 
> Does anyone have any elegant way of doing this with perhaps using
> replacement rules?
> 
> Thanks much,
> 
> 
> Brian
> 

What is elegance? Here just another way to do it:

In[2]:=
{sL, pivot, sR} = 
  Take[myString, #]& /@{Prepend[#1-1, 1],#1,Append[#1+1, -1]}& @@ 
    Position[myString, _?LowerCaseQ, {1}, Heads -> False]
Out[2]=
{{"S", "T", "M"}, {"x"}, {"R", "K", "P", "G"}}
In[3]:=
Flatten@Outer[(#1 <> pivot <> #2 &), sL, sR]
Out[3]=
{"SxR", "SxK", "SxP", "SxG", 
 "TxR", "TxK", "TxP", "TxG", 
 "MxR", "MxK", "MxP", "MxG"}

You will get no result if sL or sR is empty. Everything after the
first lowercase character will go to sR.

Hartmut Wolf



  • Prev by Date: RE: Preserve subscripts like integers using N
  • Next by Date: RE: Working with strings
  • Previous by thread: Re: Working with strings
  • Next by thread: RE: Working with strings