MathGroup Archive 2001

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

Search the Archive

Re: metacharacters as record separators


I think it would be relatively easier to perform the separation of data once
you have read your records with ReadList. This way you're already inside
Mathematica and things become smoother. Suppose you have a file of ordinary
words which you read into your notebook with ReadList:

In[1]:=
originalList = ReadList["testwords.txt", Word]
Out[1]=
{"Those", "friends", "thou", "hast", "\",\"", "and", "their", "adoption", \
"tried", "\",\"", "grapple", "them", "unto", "thy", "soul", "with", "hoops",
\
"of", "steel"}

Now suppose that you wish to separate your original records using, say, the
letter "h" as record separator:

In[2]:=
r2 = Flatten[Characters /@ originalList]
Out[2]=
{"T", "h", "o", "s", "e", "f", "r", "i", "e", "n", "d", "s", "t", "h", "o",
\
"u", "h", "a", "s", "t", "\"", ",", "\"", "a", "n", "d", "t", "h", "e", "i",
\
"r", "a", "d", "o", "p", "t", "i", "o", "n", "t", "r", "i", "e", "d", "\"",
\
",", "\"", "g", "r", "a", "p", "p", "l", "e", "t", "h", "e", "m", "u", "n",
\
"t", "o", "t", "h", "y", "s", "o", "u", "l", "w", "i", "t", "h", "h", "o", \
"o", "p", "s", "o", "f", "s", "t", "e", "e", "l"}

In[3]:=
newList = {}; While[
r2 != {}, (ct = 0; If[r2 != {}, Scan[If[# != "h", ct += 1, Return[]] &, r2];
AppendTo[newList, Take[r2, ct]];
r2 = If[Length[r2] >= ct + 1, Drop[r2, ct + 1], r2 = {}]])]

In[4]:=
StringJoin /@ newList
Out[4]=
{"T", "osefriendst", "ou", "ast\",\"andt", "eiradoptiontried\",\"grapplet",
\
"emuntot", "ysoulwit", "", "oopsofsteel"}

I'm sure something more straightforward exists, but this is what came to my
mind first.

Tomas Garza
Mexico City


----- Original Message -----
From: "Aaron E. Hirsh" <aehirsh at leland.stanford.edu>
To: mathgroup at smc.vnet.net
Subject: [mg26935] [mg26881] metacharacters as record separators


> Is it possible to use metacharacters as part of record separators in
> ReadList? For example, can one separate data at each occurrence of a
> number, or at each occurrence of a character other than an uppercase
> letter?
>
> Many Thanks,
>
> Aaron Hirsh
> --
>
>
>
> Aaron E. Hirsh
> Center for Computational Genetics and Biological Modeling
> Stanford University
> tel. (650) 723-4952
> fax (650) 725-0180
>




  • Prev by Date: RE: Functions and Packages
  • Next by Date: Re: The Integrator (Wolfram)
  • Previous by thread: Re: metacharacters as record separators
  • Next by thread: Re: metacharacters as record separators