{ From user Lonnie, Model Flat_file_library at Wed, Jun 11, 2008 9:49 AM~~ } Softwareversion 4.1.1 Linklibrary Flat_file_library Title: Flat File Library Description: Routines for writing data to and from flat files, particu~~ larly for 2-D tables to and from comma-separated files. Author: Lonnie Chrisman Date: Thu, Mar 17, 2005 12:59 PM Saveauthor: Lonnie Savedate: Wed, Jun 11, 2008 9:49 AM Defaultsize: 48,24 Nodeinfo: 1,1,1,1,1,1,0,0,0,0 Diagstate: 1,316,25,550,300,17 Fontstyle: Arial, 15 Fileinfo: 0,Linklibrary Flat_file_library,2,2,0,0,W:\Analytica\ExecDeb~~ ug\Libraries\Flat File Library.ANA Function Readcsvfile(filename:atomic textual ; linesep,colsep : option~~ al textual) Title: ReadCsvFile Description: Reads in a CSV file.~ The first line of the CSV should be the field names.~ Lines are separated by linesep (Chr(10) by default) and columns by co~~ lsep (comma by default).~ ~ The result will be an array of STRINGS. You must convert them to num~~ bers separately. They will be indexed by two local indexes: Field a~~ nd Row. Definition: ParseCsvText(ReadTextFile(filename),linesep,colsep) Nodelocation: 88,48,1 Nodesize: 60,24 Windstate: 2,188,146,517,447 Paramnames: filename,linesep,colsep Function Parsenum(x) Title: ParseNum Description: Parses text to a numeric value, for example:~ ParseNum("3.14") --> 3.14 Definition: Coerce_to_numeric(x) Nodelocation: 88,120,1 Nodesize: 60,24 Windstate: 2,313,237,476,224 Paramnames: x Numberformat: 1,D,4,2,0,0 Function Parsedate(x) Title: ParseDate Description: Converts a textual date to an integer representing the # ~~ of days since the date origin, either 1 Jan 1904 or 1 Jan 1900 (the l~~ atter when the "Use Excel Date Origin" preference is checked). Definition: Coerce_to_numeric(x) Nodelocation: 208,120,1 Nodesize: 60,24 Paramnames: x Numberformat: 1,DD,2,2,0,0 Function Writecsvfile( Filename : atomic textual ;~ A : ArrayType[Field,Row] ; Field,Row : IndexType ; ~ sep,lineSep : optional atomic textual ;~ warn : optional atomic boolean ) Title: WriteCsvFile Description: Write a two-dimensional array to a CsvFile.~ The Field dimension is written on a single line separated by commas.~ The Row dimension has each element on a separate line.~ The first line of the file contains the names of the I index elements~~ .~ ~ Warning: You should make sure that your array A does not contain the ~~ sep or lineSep strings, or if it does, you should use alternative (pe~~ rhaps multi-character) separators to avoid ambiguity.~ ~ Numbers are written in an exponential full-precision format. To use ~~ an alternative format, caller can coerce to strings before calling, f~~ or example use ""&A as the A parameter and set the caller's number fo~~ rmat to the desired output format. If you do that, be careful not to~~ include thousand separators if sep is a comma. Definition: if IsNotSpecified(sep) then sep := ",";~ If IsNotSpecified(lineSep) then lineSep := chr(10); { LF }~ If IsNotSpecified(warn) then warn:=false;~ ~ { First, write the column headers }~ WriteTextFile( filename,~ JoinText( '"' & Field & '"', Field,sep )&lineS~~ ep,~ warn:warn );~ ~ { Next, write the 2-D data: }~ ~ WriteTextFile( filename, ~ JoinText( A, Field, sep ), ~ append:true, ~ warn:warn,~ sep : lineSep ) Nodelocation: 208,48,1 Nodesize: 60,24 Windstate: 2,135,19,554,676 Paramnames: Filename,A,Field,Row,sep,lineSep,warn Numberformat: 1,F,15,14,0,0 Module Flat_file_internals Title: Flat File Internals Author: Lonnie Date: Wed, Aug 08, 2007 1:00 PM Nodelocation: 88,184,1 Nodesize: 48,24 Function Coerce_to_numeric(x : coerce numeric) Title: Coerce to Numeric Description: When x is text, parses it to a numeric value based on the~~ calling object's number format. If the caller uses a date format, t~~ hen a date can be parsed, etc. If x is already numeric, x is returne~~ d. Definition: x Nodelocation: 96,48,1 Nodesize: 48,24 Windstate: 2,454,371,476,224 Paramnames: x Close Flat_file_internals Function Parsecsvtext(s:atomic textual ; linesep,colsep : optional tex~~ tual) Title: ParseCsvText Description: Parses the textual contents of a CSV file. You provide t~~ he actual CSV contents -- already obtained from a flie or other data ~~ source.~ ~ The first line of the CSV should be the field names.~ Lines are separated by linesep (Chr(10) by default) and columns by co~~ lsep (comma by default).~ ~ The result will be an array of STRINGS. You must convert them to num~~ bers separately. They will be indexed by two local indexes: Field a~~ nd Row. Definition: if IsNotSpecified(linesep) then linesep := Chr(10);~ if IsNotSpecified(colsep) then colsep := ",";~ var file := split(s,linesep);~ var n:=size(file);~ while (n>1 and slice(file,n)="") do n:=n-1;~ index Row := 1..n-1;~ index Field := Split(Slice(file,1),colsep);~ for line[] := Slice(file,Row+1) do Array(Field,Split(line,colsep)) Nodelocation: 328,48,1 Nodesize: 60,24 Paramnames: s,linesep,colsep Close Flat_file_library