> --Original Message-- On Behalf Of [EMAIL PROTECTED]
> I inherited a big bunch of space-separated files. Are there 
> verbs dealing with space-separated value data files in the 
> same manner as readcsv? 

If you want something simple then the following will probably work.

chopspcdelim=:(<;._2~ e.&(32 10{a.));.2 @ toJ 

   fl=:'39.43 499 3999 Thisisthetext secondtext 44'
   ]fl=:fl,CRLF,'33 403 20.49 3992.3 4 32',CRLF
39.43 499 3999 Thisisthetext secondtext 44
33 403 20.49 3992.3 4 32

   chopspcdelim fl
+-----+---+-----+-------------+----------+--+
|39.43|499|3999 |Thisisthetext|secondtext|44|
+-----+---+-----+-------------+----------+--+
|33   |403|20.49|3992.3       |4         |32|
+-----+---+-----+-------------+----------+--+

Alternatively below is an edited version of csv.ijs that defaults to
using commas, but lets you specify an alternative delimiter.

   fl2=:'39.43 499 3999 "This is the text" secondtext 44'
   ]fl2=:fl2,CRLF,'33 403 20.49 3992.3 4 32',CRLF
39.43 499 3999 "This is the text" secondtext 44
33 403 20.49 3992.3 4 32

   ' ' fixcsv fl2  NB. For a file use: ' ' readcsv filename 
+-----+---+-----+----------------+----------+---+
|39.43|499|3999 |This is the text|secondtext|44 |
+-----+---+-----+----------------+----------+---+
|33   |403|20.49|3992.3          |4         |32 |
+-----+---+-----+----------------+----------+---+

NB. =========================================================
NB. csv.ijs edited to allow alternative delimiter
NB. appendcsv & writecsv still need some work to accept 
NB. alternative delimiters.
NB. =========================================================
NB. read/write comma-separated value data (*.csv) files

NB. main definitions:
NB.  dat appendcsv file  - appends boxed array to a csv file 
NB.      readcsv file    - reads data into a boxed array
NB.  dat writecsv file   - writes a boxed array to a csv file

require 'files strings'

cocurrent 'z'

NB. =========================================================
extcsv=: , #&'.csv' @ (0: = '.'"_ e. (# | i:&PATHSEP_j_) }. ])

NB. =========================================================
chopcsv=: 3 : 0
',' chopcsv y
:
dat=. y,x
b=. dat e. x
c=. ~:/\dat='"'
msk=. b>c
if. 0=+/msk do. msk=. (#msk){.1 end.
dat=. msk <;._2 dat
b=. '"'={.@(1&{.) &> dat
dat=. b }.each dat
b=. '"'={.@(_1&{.) &> dat
dat=. (-b) }.each dat
)

NB. =========================================================
NB.*appendcsv v appends a boxed array to a csv file
appendcsv=: 4 : 0
dat=. makecsv x
dat fappends extcsv y
)

NB. =========================================================
NB.*fixcsv v convert csv data into J array
fixcsv=: 3 : 0
',' fixcsv y
:
b=. y e. LF
c=. ~:/\y='"'
msk=. b > c
> msk <@(x&chopcsv) ;._2 y
)

NB. =========================================================
NB.*makecsv v takes boxed array and creates csv string
makecsv=: 3 : 0
',' makecsv y
:
dat=. ,each 8!:2 each y
f=. '"'&,@(,&('"',x))@(#~ >:@(=&'"'))
dat=. f each dat
f=. <@(,&LF)@}:@;
dat=. ;f"1 dat
)

NB. =========================================================
NB.*readcsv v reads csv file into a boxed array
readcsv=: 3 : 0
',' readcsv y
:
dat=. freads extcsv y
if. dat -: _1 do. return. end.
x fixcsv dat
)
 
NB. =========================================================
NB.*writecsv v writes a boxed array to a csv file
writecsv=: 4 : 0
dat=. makecsv x
dat fwrites extcsv y
)

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to