I posted code to parse a CSV string into a nested array containing the
elements.  It correctly handles surrounding quotes, embedded quotes, and
empty fields.  I am posting a GNU APL specific one here.

      ∇CSV∆Parse[⎕]∇
    ∇
[0]   z←CSV∆Parse v;y;d;q
[1]   d←','
[2]   q←'"'
[3]  ⍝ break apart columns into separate arrays, normalize embedded quotes,
and remove surrounding quotes
[4]
z←{((⍴⍵)⍴y,((0⌈¯2+⍴⍵)⍴1),y←~(q=¯1↑⍵)∧(q=1↑⍵)∧1<⍴⍵)/⍵←(-y)↓(⍲/(¯1+⍳⍴⍵)⌽(0
¯1↓0,0 1↓(((⍴⍵)⍴q)∘.=⍵)),0)/⍵←⍵,(y←1=⍴,⍵)⍴'
'}¨z←1↓1↓¨(1++\(z∊d)>≠\z∊q)⊂z←(1↑d),(1↑d),v
    ∇

      CSV∆Parse 'abc,,"def","abc""def"'
┌→────────────────────────┐
│┌→──┐ ┌⊖┐ ┌→──┐ ┌→──────┐│
││abc│ │ │ │def│ │abc"def││
│└───┘ └─┘ └───┘ └───────┘│
└∊────────────────────────┘

Reply via email to