all groups > flash data integration > march 2005 >
You're in the

flash data integration

group:

Loading an array from an external file


Loading an array from an external file SimonTheSwift
3/30/2005 10:28:28 PM
flash data integration:
It's about "asymmetry" in LoadVars(). I want to load an array, but didn't know
how to actually story it in the external file. So I used LoadVars.toString() to
show me the format and when I used it, it doesn't work. I guess I am missing
some step. I am attaching the code.



/* Asymmetry in LoadVars() */

/* Idea: I want to load an external variable, in this case an
array, from an external file (data.txt), using LoadVars().

The question is how to store an array in the external file?

I use the LoadVars.toString() method to see how LoadVars()
stores an ActionScript array */

// First, I create the array
var myArray1:Array = new Array("cat","dog","pig");

// Then I create the variable using LoadVars()
var myVars1:LoadVars = new LoadVars();
myVars1.collection = myArray1;

// Then I use LoadVars.toString() to see how to store it
trace(myVars1.toString());
// The output is
// collection=cat%2Cdog%2Cpig

/* Logically, if I store the above output into data.txt and
use LoadVars() to load the array variable into an ActionScript
array, it should work. */

var myVars2:LoadVars = new LoadVars();
var myArray2:Array = new Array();
myVars2.onLoad = function(ok) {
if(ok) {
myArray2 = myVars2.collection;
}
}
myVars2.load("data.txt");
trace(myArray2);
// There is no output

/* When checking the Debug->List Variables, this is what gets stored
in myArray2:

Variable _level0.myArray2 = "cat,dog,pig\r\n"

If one uses trace(myArray2), there is no output...

====================================================================

The question is, why isn't this process symmetric? I asked the
method itself to show me how to store an array, and when I use
that format, it does not load it */
Re: Loading an array from an external file SimonTheSwift
3/30/2005 11:30:54 PM
Ok, I was working more on the problem of loading an array from an external file
using LoadVars(), and found a different solution:

Let's say that the external file contains the following

collection=cat,dog,pig

When using LoadVars(), Flash loads it as this (using Debug->Variables List)

collection:"cat,dog,pig"

Which is a String. So, it is possible to use the following code

myArray = myVars.collection.toString().split(",");

to change String into Array.
AddThis Social Bookmark Button