> From: emptist
> In q I've learned to read a kind of binary
> formatted historical price data file, which contains 8 4 byte int data
> for each record, using:
>
> list: ("iiiiiiii";4 4 4 4 4 4 4 4) 1: `:path/filename
>
> and this will give me a list of data of the entire file. (For those not
> familiar with q, 1: reads binary file, and i stands for int type in q,
> while 4 means a field is 4 byte in size).
Putting together the nicely broken down steps that Roger gave, a J translation
of the above line of q is:
_8]\ _2(3!:4) 1!:1 <'path/filename'
If the fields in each record weren't all of the same type, then it would be
more involved.
Below is just an example of roundtripping a table of integers to binary file
and back again.
]tst=: 5 8 ?...@$ 50 NB. Create 5 by 8 table of integers
46 5 29 2 4 39 10 7
10 44 46 28 13 18 1 42
28 10 40 12 31 16 10 14
14 21 13 3 26 26 25 27
18 48 42 41 49 47 49 9
tstchar=: 2 (3!:4) ,tst NB. convert to sets of 4 bytes
tstchar 1!:2 <jpath '~temp/my.bin' NB. write to file
_8]\ _2 (3!:4) 1!:1 <jpath '~temp/my.bin' NB. read back integer table
46 5 29 2 4 39 10 7
10 44 46 28 13 18 1 42
28 10 40 12 31 16 10 14
14 21 13 3 26 26 25 27
18 48 42 41 49 47 49 9
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm