External Tab-Delimited File to a List

Date:    Mon, 1 Jan 1996 12:25:14 GMT
From:    Andreas Viviani <aviviani@ACCESS.CH>
Subject: Re: External Text File to List (long LINGO sample)

>Has anyone been successful at bringing in an external text
>file (tab-delimited) into a list?
Here the LINGO for READ (You may WRITE, too, but please derive it from this code yourself):
on fileError errType
  -- general handler to terminate program after File IO error
  -- NEEDS A quitApp HANDLER (It really is a good practice)

  set errMessList= [ 
  33:"File directory full", 
  34:"Volume full", 
  35:"Volume not found", 
  36:"I/O-Error", 
  37:"Bad file name", 
  38:"File not open", 
  42:"Too many files open", 
  43:"File not found", 
  56:"No such drive", 
  65:"No disk in drive", 
  120:"Directory not found"]

  set errMess= getAProp( errMessList, abs( errType ) )
  if( string( errMess )= EMPTY ) then set errMess= "unknown"
  set errMess= "(" & QUOTE & errMess & QUOTE & ")"
  alert "File IO Error"&&errType&&errMess&"; program terminated."

  quitApp  -- replace with "quit" if you can't resist...

end fileError


on openDataFile dataFileName
  global hDataFile

  set hDataFile= fileIO( mNew, "READ", dataFileName&".DAT" )
  if( not( objectP( hDataFile ) ) ) then
    if( hDataFile <> -43 ) then fileError( hDataFile )
    -- THE dataList WILL BE EMPTY IF THE FILE IS NOT FOUND (new data!):
    exit
  else
    set s= hDataFile( mReadLine )
    -- THE FIRST LINE IS A FILENAME/TYPE IDENTIFYER FOR SECURITY:
    if( word 1 to 3 of s )= dataFileName&&"data file" then
      readData
    else
      alert "Unexpected first line in"&&dataFileName&".DAT. No input
accepted." 
      && "The file may be corrupted or renamed accidentally."
    end if
  end if

  hDataFile( mDispose )
end openDataFile


on readData
  global hDataFile, dataList

  set f= hDataFile( mReadFile )
  -- f is a local string containing the file now.
  -- This method is much faster then mReadLine
  if f= EMPTY then exit

  set l= the number of lines in f
  if l < 1 then exit
  repeat with i= 1 to l
    set d= []
    set s= line i of f
    repeat with j= 1 to the number of items in s
      add d, item j of s
      -- THE DATA IS ADDED AS TEXT; YOU MAY MODIFY HERE
      -- FOR SPECIFIC HANDLING OF "COLOUMNS"
    end repeat
    add dataList, d
  end repeat

end readData


on startMovie
  global dataList

  set dataList= []
  set the itemDelimiter= TAB
  openDatafile "DATA"
  put dataList  -- DEB; remove in real app.

end startmovie

Remember, for this sample, a file DATA.DAT containing 3 data sets should look like:

DATA data file 01-JAN-1996 1 2 3 4 5.41437 gaga mega wuff 3 4 5 6 7 8 9 0.01
(More than one SPACE represent a TAB) And you may easily change the data delimiter or the file name.