⍝ ⍝ Author: Alex Weiner ⍝ Date: June 27, 2015 ⍝ Copyright: Copyright (C) 2015 by Alex Weiner ⍝ License: LGPL see http://www.gnu.org/licenses/gpl-3.0.en.html ⍝ Support email: alexweiner@alexweiner.com ⍝ Portability: L3 (GNU APL) ⍝ ⍝ Purpose: ⍝ Read in a text file, with a given field separator and return a ⍝ 2D matrix of text vectors ⍝ ⍝ ⍝ Create a text matrix, with a given separator and ⍝ every element will be quoted. ⍝ ⍝ Description: ⍝ parsed_csv← ',' read∆csvfile 'name of the file' ⍝text_csv_matrix←','make∆csv matrix_parsed_csv ⍝ a matrix of text vectors ⍝ ⍝ )COPY 5 FILE_IO.apl file_matrix_ucs←{⊃(⎕ucs ⍵)⊂⍨~(⎕ucs ⍵)∊10 11} ∇parsed_row←sep read∆csv∆row string inquotes_bool←{⍵∨≠\⍵}34= string sep_bool←string= ⎕ucs sep sep_idx←sep_bool/⍳⍴sep_bool sep_in_quotes_idx← (sep_idx∊inquotes_bool/⍳⍴inquotes_bool)/⍳⍴sep_idx sep_idx_real←sep_idx~sep_idx[sep_in_quotes_idx] sep_bool_real←0⍴⍨⍴string sep_bool_real[sep_idx_real]←1 parsed_row←string⊂⍨~sep_bool_real ⍝now find the bookend quotes and drop em size_each_item← ⍴¨parsed_row quotes_expected←⌽¨1,¨⌽¨1,¨2↓¨size_each_item⍴¨0 quotes_found←parsed_row=34 ⍝34←→ '"' hasquotes_bool← ∨/¨quotes_expected∧quotes_found parsed_row[hasquotes_bool/⍳⍴hasquotes_bool]←⌽¨1↓¨⌽¨1↓¨parsed_row[hasquotes_bool/⍳⍴hasquotes_bool] parsed_row←⎕ucs¨parsed_row ∇ ∇matrix←sep read∆csvfile file matrix←⊃sep read∆csv∆row¨⊂[2] file_matrix_ucs FIO∆read_file file ∇ ∇filecontents ←sep make∆csv matrix seps←⍉(⍴matrix)⍴⊂,sep withquotes←⍉(⍴matrix)⍴⌽¨(⊂,'"'),¨⌽¨(⊂,'"'),¨matrix filecontents←⍕⍉((2 1 × ⍴withquotes)-1 0)↑(2 1 ×⍴withquotes)⍴withquotes,seps ∇ ]NEXTFILE ⍝ ⍝ EOF