Oleg Kobchenko wrote:
> We need a general purpose read line functionality.
> It is common in C runtime and in other languages.
> Although, it is possible to do in J, but it's better not
> to do the low-level stuff every time.

I suggest that we add two new definitions to the files script. One is
Joey's verb to read a LF-terminated block from a file, the other is
Oleg's adverb to apply a function to each line of a file.

In each case, the file is assumed to be in lines terminated by LF, and a
trailing LF is assumed if not present. CR is removed. Blocksize is
hardcoded at 1e6.

Definitions are:

NB.*freadblock v read block from file
NB. y is filename;start position
NB. returns: block;new start position
freadblock=: 3 : 0
'f p'=. y
f=. 8 u: f    NB. for j601
s=. 1!:4 <f
if. s = _1 do. return. end.
if. p < s do.
  dat=. 1!:11 f;p,1e6<.s-p
  len=. 1 + dat i: LF
  p=. p + len
  if. len > #dat do.
    if. p > s do.
      dat=. dat, LF
    else.
      'file not in LF-delimited lines' 13!:8[3
    end.
  else.
    dat=. len {. dat
  end.
else.
  dat=. ''
end.
(dat -. CR);p
)

NB.*fapplylines a apply verb to lines in file delimited by LF
fapplylines=: 1 : 0
y=. 8 u: y    NB. for j601
s=. 1!:4 <y
if. s = _1 do. return. end.
p=. 0
while. p < s do.
  dat=. 1!:11 y;p,1e6<.s-p
  len=. 1 + dat i: LF
  p=. p + len
  if. len > #dat do.
    if. p > s do.
      dat=. dat, LF
    else.
      'file not in LF-delimited lines' 13!:8[3
    end.
  else.
    dat=. len {. dat
  end.
  u ;._2 dat -. CR
  p=. s
end.
)

With these definitions, Yoel's problem would have solutions like the
following:

getcsn=: 3 : 0
ptr=. 0
res=. i. 0 0
while.
  'dat ptr'=. freadblock y;ptr
  # dat=. <;._2 dat do.
  res=. ~. res, 4 }."1 > dat #~ (<'csn ') = 4 {. each dat
end.
)

readcsn=: 3 : 0
CSN=: i.0 0
readcsn1 fapplylines y
CSN
)

readcsn1=: 3 : 0
if. 'csn ' -: 4 {. y do. CSN=: ~. CSN, 4 }. y end. 0
)
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to