Your problem is likely already solved, but this may also be of benefit...

1. (load 'files') to access some pre-canned scripts for files.
   In particular, fread, fwrite and fdir.

2. You can just fread the file into a J noun ...
   'b' fread 'in.txt'   NB. Returns a boxed result (one box per row)
   'm' fread 'in.txt'   NB. Returns a character matrix (one row per record)

3. Sort can apply to any J noun (numeric, character, boxed) ...
   /:~ 3 3$'tictactoe'
tac
tic
toe

So you can do what you want in the following steps ...
   data =: 'm' fread 'in.txt'    NB. Read in data as matrix
   data =: /:~ data              NB. Sort the rows (of the char matrix)
   data =: data,"1 LF            NB. Append LF to each row
   data fwrite 'out.txt'         NB. Write out to new file

Or in one line as ...
   ((/:~ 'm' fread 'in.txt') ,"1 LF) fwrite 'out.txt'

The commute adverb ~ (flips left and right arguments of a verb) can be used
here to reduce the need for the parens () above...

   'out.txt' fwrite~  LF,"1~ /:~ 'm' fread 'in.txt'

If you need to write back out with both a CR and LF, then you can do ...

   'out.txt' fwrite~  (CR,LF),"1~ /:~ 'm' fread 'in.txt'

The above use of 'character sort' obviates the need to convert character to
numeric in this simple example you gave.

COMMENT 1:
If you need to apply a numeric verb, then to convert the character to
numeric you can use ". (the J verb Do) as follows ...
   ". '3 45 678'                  NB. 8 characters
3 45 678                          NB. 3 integers

You should review the J Vocabulary page for ". for more details (from Help
menu), as you can use the dyadic (left argument) case also to control the
conversions (eg to allow for a 'bad' number in a string)...
   ". '34 4t5  56'
Ill-formed number                 NB. J error

   _999 ". '34 4t5 56'            NB. 9 characters (one bad number)
34 _999 56                        NB. 3 integers

COMMENT 2:
If you are using J as an 'intermediate process', then for large data sets
you may consider writing from the source application in a binary format (ie
32 bit integers as 4 bytes, 64 bit floats as 8 bytes etc).

J can read and write these files in binary formats using another utility
library (load 'nfiles').

Note: You can type (load 'scriptdoc'), then (scriptdoc 'files') and
(scriptdoc 'nfiles') to see more details on these utility scripts.

COMMENT 3:
For general help on 'conversions' between character and numeric formats
within J, you can also see the page on "Foreign Conjunctions" (3!:) on the
Help Menu.


I realise you have done much of this already, but some of these things are
not so obvious to discover for yourself.

I hope this helps, Rob Hodgkinson


On 1/2/08 3:06 PM, "PackRat" <[EMAIL PROTECTED]> wrote:

> Ric Sherlock wrote:
>> If you give a simple example of the initial CRLF-separated lists and the
>> format of a vector post-processing it may be possible to simplify
>> further.
> 
> Thanks to both you and Henry for your responses!
> 
> OK, here's a simple example:
> 
> Diskfile:    Vector: . . . . . . . . .     Diskfile:
> 
> 3<CR><LF> --> 3 1 2 --> sort* --> 1 2 3 --> 1<CR><LF>
> 1<CR><LF>                                   2<CR><LF>
> 2<CR><LF>                                   3<CR><LF>
> 
> * or other operations, particularly set operations (and, or, not,
> dedupe, etc.)
> 
> The  <CR><LF>  <-->  <LF>  conversions are assumed above.
> 
> Essentially, I am calling the J server from a script written in another
> language, so that I can use J's fast array processing for handling one
> or two vanilla ASCII files exported from another application, and then
> creating a new vanilla ASCII file to reimport into that application.  I
> hope that gives some more context.  Thanks again!
> 
> Harvey
> 
> 
> ----------------------------------------------------------------------
> 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