I am reading data from a txt file using loadVars. By default my text file must have an ampersand starting the text file a variable that = the text content and an apersand ending the the text file.
like this
&text=the content&
I would like to use a different slipt symbol like the “|”. I know this can be done for arrays in flash but how about for reading textfiles?
loader = new LoadVars();
loader.onData = function(rawText){
// split raw text by divider adding
// to a data variable (array)
var data = rawText.split("|");
// get any rid of empty array values
// at the ends of the array
if (data[0] == "") data.shift();
if (data[data.length-1] == "") data.pop();
// declare a new array to hold the
// variable - value pairs
var variables = new Array();
// loop through the data array and
// assign the variables array by splitting
// the data by = to get variables and values
var i = data.length;
while(i--) variables.unshift(data*.split("="));
// loop through the variables array to physically
// assign the values to the loadVars object
i = variables.length;
while(i--) this[variables*[0]] = variables*[1];
// call the onLoad (if defined) with success true
this.onLoad(true);
// test variables
trace(this.myVar1); // value1
trace(this.myVar2); // value2
}
loader.load("data.txt");
/* data text contents:
|myVar1=value1|myVar2=value2|
end data text contents */