Page 1 of 1

loading an array from an external text file

Posted: Mon Mar 23, 2009 1:01 am
by magice
I have figured out (thanks to a search of these forums) how to save a combined array to a text file. Now, I need to know how to make an "open" dialog box pop up, and how to read the data in a chosen text file and put it back into a variable for splitting. any help would be greatly appreciated

Posted: Mon Mar 23, 2009 7:23 am
by Janschenkel
This can be as simple as

Code: Select all

on mouseUp
  local tFilePath, tData, tLine, tItem
  local tArray, tRowCounter, tColumnCounter
  answer file "Open tab-delimited text file"
  if it is empty then exit mouseUp
  put it into tFilePath
  put URL ("file:" & tFilePath) into tData
  put 0 into tRowCounter
  set the itemDelimiter to tab
  repeat for each line tLine in tData
    add 1 to tRowCounter
    put 0 into tColumnCounter
    repeat for each item tItem in tLine
      add 1 to tColumnCounter
      put tItem into tArray[tRowCounter,tColumnCounter]
    end repeat
  end repeat
end mouseUp
HTH,

Jan Schenkel.

Posted: Mon Mar 23, 2009 7:42 am
by FourthWorld
Stacks are files too, and natively support arrays in custom properties.

You can save yourself some parsing time by just using stack files for you file format:

set the customProperties of tMyDataStack to MyArray
put the customProperties o tMyDataStack into tMyArray