Read a text file

Ken Ray kray at
Mon Jan 17 22:56:37 EST 2005

On 1/17/05 9:05 PM, "Richard Gaskin" <ambassador at> wrote:

> I don't have the url offhand (in a bit of a rush this evening), but it
> wasn't hard to find last time I looked it up.

Here's the relevant code from Alex (something I've had in Scripter's
Scrapbook for a while now in case I need it):


function CSV2Tab3 pData
   local tNuData -- contains tabbed copy of data

   local tReturnPlaceholder
   -- replaces cr in field data to avoid line breaks which would be misread
   -- as records; replaced later during display

   local tEscapedQuotePlaceholder
   -- used for keeping track of quotes in data

   local tInQuotedText
   -- flag set while reading data between quotes
   put numtochar(11) into tReturnPlaceholder -- vertical tab as placeholder
   put numtochar(2)  into tEscapedQuotePlaceholder
   -- used to simplify distinction between quotes in data and those in
   -- delimiters
   -- Normalize line endings:
   replace crlf with cr in pData          -- Win to UNIX
   replace numtochar(13) with cr in pData -- Mac to UNIX
   -- Put placeholder in escaped quote (non-delimiter) chars:
   replace ("\"&quote) with tEscapedQuotePlaceholder in pData
   replace quote&quote with tEscapedQuotePlaceholder in pData
   put space before pData   -- to avoid ambiguity of starting context
   split pData by quote
   put False into tInsideQuoted
   put 0 into tCounter
   repeat for each line tKey in (the keys of pData)
     add 1 to tCounter
     put pData[tCounter] into k
     if (tInsideQuoted) then
       replace cr with tReturnPlaceholder in k
       put k after tNuData
       put False into tInsideQuoted
       replace comma with tab in k
       put k after tNuData
       put true into tInsideQuoted
     end if
   end repeat
   delete char 1 of tNuData -- remove the leading space
   replace tEscapedQuotePlaceholder with quote in tNuData
   return tNuData
end CSV2Tab3


Ken Ray
Sons of Thunder Software
Web site:
Email: kray at

More information about the Use-livecode mailing list