Extracting arrays from textfile

I have a variable which takes a particular form of array (used with the component DataSet). It looks like this:

var recData = [{svenska:"Hej", spanska:"Hola"}, {svenska:"nej", spanska:"no"}, {svenska:"också", spanska:"también"}];

I want to extract this variable from a textfile instead of writing it directly into the code-area of the flash-file, so I used the following code:

// 1) Loads the textfile, recieves the array, and puts it into a variable called txt_Data
var txt_DataSet = new LoadVars(); 
txt_DataSet.onLoad = function(success) { 
  _root.txt_Data = this.TEXT;
// 2) Puts the recieved variable into the actual variable
var recData:Array;
recData = _root.txt_Data;

Unfortunately it seems the array gets converted to an ordinary string in the process, so I cant use it properly. Is there a way I could extract the array from this string again, and if so, how?


well, you’re always get a string from a text file. Keeping that in mind, you’ll want to format the text in the text file that would let you easily convert it to the array/objects as needed. How depends on what format you choose and your use of string.split() string.substr() etc.

I bet Im probably better off reading the array from an xml-file instead, right? That way I could import variables straight from the xml-file, instead of having the whole thing converted to a string first, if I have understood things correctly?

Well, better learn how to import xml-files to flash, then. Think I saw some pretty good tutorials on the subject here on Kirupa.

Thanks again, sen :slight_smile:

xml or text file, you will have to do some converting, and in both directions. You will have to convert to the file and convert after the file is loaded back into your Flash object.

xml will make it a little more organized, but will add to the complication of parsing the information when loaded.

Yeah, I guess so. ****ed if I do, ****ed if I dont :wink:

Well, I decided to learn how to implement the DataSet, DataGrid, and XMLConnection components, and found an awesome tutorial among the helpfiles called “XML Tutorial: Timesheet”.

The example-application youre left with at the end of the tutorial is very easy to modify for most uses I can think of.