I would hate to try to explain "sortfile" to a newbe. This thread is dealing with two problems - how to read and write files, and how to sort character data.
The main difference in reading and writing files in J from other languages is that J reads the entire file, where most other languages read in data one line at a time. In most other languages the data needs to be put into an array to be sorted, but line endings is not an issue. In J it's up to us to decide how to break the data into lines. And a little nastier problem to put end of line characters on data to be written. The other problem is sorting. I think that /:~ is all that's needed. Once that is understood then it is a good lead-in to boxing and 2-d arrays of characters as a ways to sort text data. On Mon, May 27, 2013 at 4:04 PM, Joey K Tuttle <[email protected]> wrote: > I think having examples in the wiki is a great idea. For many years > (starting c. 1990) my profile.ijs included my definitions of > > fread=: 1!:1@< > fwrite=: 1!:2 < > > a year or so ago I commented out my definitions in my profile so that when > I tried something for/from the forum I would be using the definitions > provided with the system. The idea of an argument to make boxes 'b' or a > matrix 'm' may be useful, but it looses a certain amount of flexibility in > answering the original question of how to sort a text file. That > flexibility is mainly lost because of the use of <;._2 (instead of <;.2) > for boxing the file. Often times, "helpful features" make more complicated > solutions (things I encounter in M$ Word and Excel leap to mind...) > > The original question of how to sort the lines of a text file could be > written, with no cover functions at all, as - > > sortfile=: ([:;[:/:~[:<;.2[:][:1!:1<)1!:**2[:<'sorted.'"_,] > > Where the output file is prefixed with 'sorted.' to avoid destroying the > original file. I'm sure that (13 : generated) tacit definition could be > made more readable - but the point is that having "convenient cover > functions" can make things obscure in a different way. Note that sortfile > completely avoids the issues around "line end character(s)" and works for > lines ending with CR, LF, or CR,LF. > > . . . > > ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
