Reading CSV Files

The CSV file is something of a standard for data interchange, a source of frustration for the newcomer to APL is that there isn't any special recognition of CSV files in and of themselves (most versions of APL lump all native files as "the same thing" and leave unravelling them as an exercise for the programmer.

Here's a small defined function that can be used to read many CSV files and return a result which is useable within APL.  It can be used as a template for more complex variants.

           ∇ (cols body)←sep ReadCSV file;⎕IO;⎕ML;tie;raw;cols;body
[1]   ⍝ Simple tool to read CSV files
[2]   ⍝ Assuming [0] titles in the first row
[3]   ⍝          [1] Fields separated by <sep>
[4]   ⍝          [2] All fields character without any field delimiters
[5]    ⎕IO ⎕ML←0 3                                    ⍝ Personal preference
[6]    tie←file ⎕NTIE 0                               ⍝ Let APL generate tie numbers
[7]    raw←⎕NREAD tie 80(⎕NSIZE tie)0                 ⍝ Read the whole file
[8]    ⎕NUNTIE tie                                    ⍝ Don't hang on to the file
[9]    raw←((~raw∊⎕UCS 10 13)⊂raw)                    ⍝ Break down into a nested vector
[10]   cols←{(~⍵∊sep)⊂⍵}↑raw                          ⍝ Strip out the column titles
[11]   body←⊃{(~⍵∊',')⊂⍵}¨1↓raw                       ⍝ Form a matrix for the actual values

Page created 22 December 2008.
Copyright © Dogon Research 2008.