Domanda

In J, after reading in a text file, how to get rid of those CR or LF or CRLF and then I can use ". without getting spelling error?

È stato utile?

Soluzione

Using the freads verb will force line endings to be LF. If you are using J6 or earlier then you will need to load the files script first (load 'files'), otherwise freads is available as part of the standard library. Then as Eelvex suggests cut ;. more specifically ;._2 can be used to divide the lines of the file using the last character in the string (in this case LF) as the delimiter:

   _99&".;._2 freads 'myfile.txt'

It is better to use Numbers (dyadic ".) to convert literals to numeric rather than Do (monadic ".). This avoids the potential security issue of "executing" code in the file and will correctly convert more variations, compare the following:

   ". '45 -34'        NB. monadic ".
11
   _99 ". '45 -34'    NB. dyadic ".
45 _34

You may find the following faster:

   _99 ". ];._2 freads 'myfile.txt'

Numbers is acting on the whole array at once rather than being run on each line individually.

Altri suggerimenti

cut ;. using the last item (fret |2) - which should be your CR/LF - while removing this item (fret _2).

a =: 0 : 0      NB. a =: (1!:1)<filename
1 2 3 4
5 6 7 8
)

*: ".;._2 a
 1  4  9 16
25 36 49 64
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top