Updated January 2021; see History
DIMX ary(n), X, 0, auto_extend
...
INPUT CSV #ch, ary()
Use the syntax above to input a CSV files into a DIMX array. It is equivalent to:
INPUT CSV #ch, ary(1), ary(2), ...
where the number of array elements specified is automatically adjusted at runtime to the number of fields on the line.
Comments
• | Requires compiler edit 854+ |
• | The ary() must be allocated with DIMX, and must be of type X. |
• | The array element size doesn't have to be 0 (i.e. dynamically sized), but if not, individual fields may be truncated as needed. |
• | For AUTO_EXTEND arrays (highly recommended), the extent will be set to the number of fields input, with 0 indicating none (probably EOF). |
• | In the case of a non auto_extend array, any fields on the input line beyond the fixed extent of the array will be discarded. |
See the fncsvutl.bsi in SOSLIB:[907,10] module for an example of using INPUT CSV into an array to convert each line of a CSV file into an ordmap.
Example
Beginning with A-Shell 6.5.1697 (see History below), you can replicate a CSV file using the following:
dimx ary(0), x, 0, auto_extend
open #1, "in.csv", input
open #2, "out.csv", output
do
input csv #1, ary()
if eof(1) then
exit
else
writecd #2, ary()
endif
loop
close #1
close #2
Note that the explicit test for eof(1) prior to the writecd is only needed to prevent an extra blank line at the end of the output file resulting from the final writecd with a zero element ary(). If a blank line at the end isn't a bother, then you could simplify the loop to:
do
input csv #1, ary()
writecd #2, ary()
loop until eof(1)
History
2021 January, A-Shell 6.5.1697: INPUT CSV #ch, ary(), where ary() is a DIMX array with the AUTO_EXTEND option, now updates the ary() extent based on the number of elements input. Previously it expanded the array extent as needed, but would not contract it. The new behavior matches the original intent and documentation.
2018 June, A-Shell 6.5.1637: An empty element is now added to the end of the array if the input data ends with a comma. So for example, the data line { one,two, } will return ARY() with an extent of 3, with the last field empty. Note that it is still the case that an empty line will return an array with a zero extent.
2018 February, A-Shell 6.5.1629: Function added to A-Shell.