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.
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
template for more complex variants.
∇ (cols body)←sep ReadCSV
 ⍝ Simple tool to read CSV files
 ⍝ Assuming  titles in the first row
 Fields separated by <sep>
 All fields character without any field delimiters
 ⎕IO ⎕ML←0
⍝ Personal preference
 tie←file ⎕NTIE
⍝ Let APL generate tie numbers
 raw←⎕NREAD tie 80(⎕NSIZE
⍝ Read the whole file
⍝ Don't hang on to the file
 raw←((~raw∊⎕UCS 10
⍝ Break down into a nested vector
⍝ Strip out the column titles
⍝ Form a matrix for the actual values
- Unless files are very large there doesn't seem to be a lot
of point doing anything aside from reading it as a whole.
file-reading would just add complication.
- Easy enough to modify the code to handle files which do not
contain column titles in the first row.
delimiters (characters which surround "character" columns) aren't
catered for, but it would be quite easy to strip them out and/or
convert purely numeric columns into numeric values.
- Nature's law suggests that the first file you try this on
will be the one that breaks the rules.
Page created 22