bug-apl
[Top][All Lists]
Advanced

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

Re: [Bug-apl] Submission for GNU APL Bits_and_Pieces directory


From: alexweiner
Subject: Re: [Bug-apl] Submission for GNU APL Bits_and_Pieces directory
Date: Mon, 29 Jun 2015 19:57:52 -0700
User-agent: Workspace Webmail 5.14.4

This revision adds the following:
1.switch from ⎕av to ⎕ucs
2. works for >2 columns
3. Left argument is the record separator. I didn't make it optional because I like the visual aid in the code rather than having to remember an arbitrary default case. 
4. On Louis' suggestion, it now supports "Quoted Data", incase your data item contains a comma
5. Against Louis' suggestion, I never count the number of delimiters and add 1. :)

Please see attached
-Alex
-------- Original Message --------
Subject: Re: [Bug-apl] Submission for GNU APL Bits_and_Pieces directory
From: Juergen Sauermann <address@hidden>
Date: Mon, June 29, 2015 3:05 am
To: address@hidden, address@hidden

Hi Alex,

thanks for offering this. I would change a few things to make that code even more useful:

- make the separator an (ideally optional) left argument of ReadTableData.
  Many data files are written in CSV (comma separated values) format which uses comma instead of space,

- use ⎕UCS instead of ⎕AV to make the code more portable between different APL interpreters,

- and maybe make it work for > 2 columns (by guessing the number of columns from the first line in the file).

/// Jürgen



On 06/27/2015 11:26 PM, address@hidden wrote:
Hi Bug-apl,
I am soliciting feedback (as per the process) of the attached APL code that will take a file  that has a layout of two columns (space separator) and return a matrix.

-Alex

⍝
⍝ Author:      Alex Weiner
⍝ Date:        June 27, 2015
⍝ Copyright:   Copyright (C) 2015 by AlexWeiner
⍝ License:     LGPL see http://www.gnu.org/licenses/gpl-3.0.en.html
⍝ email:       address@hidden
⍝ Portability: L3
⍝
⍝ Purpose:
⍝ Read in a text file, with a given record delimeter and return a 
⍝ 2D matrix of text vectors
⍝
⍝ Description:
⍝ parsed_csv← ',' read∆csvfile 'name of the file'
⍝
)COPY 5 FILE_IO.apl
file_data_ucs←{⎕UCS FIO∆read_file ⍵} 
file_matrix_ucs←{⊃⍵⊂⍨~10=⍵}
file_matrix_text←{⎕UCS file_matrix_ucs file_data_ucs ⍵}

∇parsed_row←sep read∆csv∆row string
quote_bool←'"'=string
quote_bool[quote_bool/⍳⍴quote_bool]←(+/quote_bool)⍴ 1 ¯1
inquotes_bool←+\quote_bool
sep_bool←sep=string   
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='"'
hasquotes_bool← ∨/¨quotes_expected∧quotes_found
parsed_row[hasquotes_bool/⍳⍴hasquotes_bool]←⌽¨1↓¨⌽¨1↓¨parsed_row[hasquotes_bool/⍳⍴hasquotes_bool]
∇
∇csv∆table←sep read∆csv∆matrix textmatrix
csv∆table←⊃ sep read∆csv∆row¨⊂[2] textmatrix
∇
read∆csvfile←{⍺ read∆csv∆matrix file_matrix_text ⍵}
]NEXTFILE 
⍝
⍝ EOF 

reply via email to

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