bug-apl
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Bug-apl] Function to parse a CSV string


From: Blake McBride
Subject: [Bug-apl] Function to parse a CSV string
Date: Sun, 15 Jun 2014 08:42:07 -0500

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

[Prev in Thread] Current Thread [Next in Thread]