MathGroup Archive 2006

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

Search the Archive

Re: Split a file into multiple files using a pattern

  • To: mathgroup at smc.vnet.net
  • Subject: [mg69690] Re: Split a file into multiple files using a pattern
  • From: Paul Abbott <paul at physics.uwa.edu.au>
  • Date: Thu, 21 Sep 2006 07:29:09 -0400 (EDT)
  • Organization: The University of Western Australia
  • References: <eeqqnq$ooo$1@smc.vnet.net>

In article <eeqqnq$ooo$1 at smc.vnet.net>, "Raj" <rajanikanth at gmail.com> 
wrote:

> COuld somebody please tell me how can I do this in Mathematica: I have
> a file of numbers containing -999.99 in places where there is missing
> data.
> I want to split the file into a number of files based on the criterion
> that the number of consecutive - 999.99 are more than 18 .
> 
> For example... if the file is of the form
> 
> {1,2,3,4,{-999.99 repeated 18 or more times},5,6,7,8}
> then I want the output to be file1.txt which contains {1,2,3,4} and
> file2.txt which contains {5,6,7,8}

Here is one approach using Split (twice):

  data = Join[
    {1, 2, 3, 4}, 
    Table[-999.99, {20}],  
    {5, 6, 7, 8},
    Table[-999.99, {25}], 
    {1, 2, 3}
  ];

  Split[data, #1 == -999.99 && #2 == -999.99 & ]

  % /. {c:-999.99..} /; Length[{c}] > 18 -> {}

  Flatten[#, 1] & /@ DeleteCases[Split[%, #1 != {} && #2 != {} & ], {{}}]

Cheers,
Paul

_______________________________________________________________________
Paul Abbott                                      Phone:  61 8 6488 2734
School of Physics, M013                            Fax: +61 8 6488 1014
The University of Western Australia         (CRICOS Provider No 00126G)    
AUSTRALIA                               http://physics.uwa.edu.au/~paul


  • Prev by Date: does the following code shut down anyone else's kernel? - why?
  • Next by Date: Re: Re: General--Mathematica and Subversion
  • Previous by thread: Re: Split a file into multiple files using a pattern
  • Next by thread: Re: Split a file into multiple files using a pattern