One issue is that J's ~ convention is not the OS convention, so you
will need to deal with that.  If you are not on windows (which is
probably the case if you are using a ~ OS convention), you can use

HOME=: 2!:5'HOME'
INFILE=: HOME,'/Desktop/infile.txt'
OUTFILE=: HOME,'/DESKTOP/outfile.txt'

With that, here's an approach which should work, for the sorting:

   (}.;/:~<;.1 LF,1!:1<INFILE) 1!:2<OUTFILE

you could also use library routines, if you prefer (like other people outlined).


On Sun, May 26, 2013 at 5:32 PM, Don Guinn <[email protected]> wrote:
> sortandprint=:4 : 0
>
> text=.'b' fread y
>
> text=./:~text
>
> text=.text,&.>LF
>
> (;text) fwrite x
>
> )
>
>
> The 'b' option means box each line.
>
>
> y is file to read,
>
> x is file to write sorted.
>
>
> If you are dealing with windows and need CRLF as a line separator, replace
> the LF with <CRLF.
>
>
> On Sun, May 26, 2013 at 1:50 PM, Murray Eisenberg 
> <[email protected]>wrote:
>
>> I'm really out of practice with J, and the meager-to-nonexistent
>> documentation is not helping me. I have a very simple task to perform. I
>> have a text file infile.txt on my desktop and here's what I want to do:
>>
>>  (1) read the file as lines (so I don't get all the end-of-line symbols);
>>  (2) sort the file's lines (alphabetically)
>>  (3) write out the result as a new text file, say outfile.txt, also on my
>> desktop.
>>
>> I can do this trivially in Mathematica:
>>
>>  t = Import["~/Desktop/infile.txt", "Lines"]
>>  Export["~/Desktop/outfile.txt", Sort[t]]
>>
>> But how would I do this in J (which, for longer files, ought to be faster)
>> and how would I discover how to do it? I prefer to use named library
>> functions where applicable.
>>
>> I tried the following in J801, after looking into text.ijs from J602:
>>
>>   load 'files'
>>   t=. fread '~/Desktop/infile.txt
>>   load 'text'
>>   cuttext t
>> | value error: cuttext
>>
>> What happened there?
>>
>> [Hope this is not too basic for this forum (as compared with
>> [email protected]).]
>>
>> ---
>> Murray Eisenberg                                    [email protected]
>> Mathematics & Statistics Dept.
>> Lederle Graduate Research Tower            phone 413 549-1020 (H)
>> University of Massachusetts                               413 545-2838 (W)
>> 710 North Pleasant Street                         fax   413 545-1801
>> Amherst, MA 01003-9305
>>
>>
>>
>>
>>
>> ----------------------------------------------------------------------
>> For information about J forums see http://www.jsoftware.com/forums.htm
>>
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to