Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Text file to multidimensional array 2

Status
Not open for further replies.

Sleidia

Technical User
May 4, 2001
1,284
FR
Hi guys :)

Let's say you have a text file with data organized like this , with pipes as delimiters :

Code:
a|1|2|3
b|4|5|6
c|7|8|9
...

With Actionscripting, how would you import the text file and build a multidimensional array like this ...

Code:
my_array[a] = array(1,2,3);
my_array[b] = array(4,5,6);
my_array[c] = array(7,8,9);

... knowing that the number of rows and columns is not know in advance?

Thanks for the help! :)
 
Okay... first of all, let's just say that instead of line breaks, we've got semicolons delimiting the rows. So, we've got

Code:
R1C1|R1C2|R1C3;
R2C1|R2C2|R2C3;
R3C1|R3C2|R3C3

Okay, I'm just going to assume you know how to get the data from that file into a string. If you don't, there's some extensive sample code in the Flash help. Anyway, that being done...

Code:
//so you've loaded the file into a string called dataString

//now we split into an array of strings that look like this:
//"R1C1|R1C2|R1C3"

var tempArray:Array = dataString.split(";");

var returnArray:Array = new Array(tempArray.length);
//cycle through the array items and split each of them
for(i=0;i<=tempArray.length-1;i++)
{
	returnArray[i]=tempArray[i].split("|");
}

return returnArray;

Technically, this is not returning a multidimensional array, it's returning a jagged array - an array of arrays. This means you don't access an element using returnArray[x,y]; instead, you access it using returnArray[x][y]. This gives you the freedom to have rows of differing lengths.

Let me know if you need any more help.
 
Thanks gormster :)

Actually, I've also great difficulties to store the whole file content into a variable.

All the scripts I've found don't seem to work because tracing the variable outputs either "undefined" or just nothing.

I don't get what's wrong. I'm certain that the text file is correctly located.

My latest try was with this :

Code:
var lv:LoadVars = new LoadVars();

lv.onData = function(thetext:String) {

var_text_string = thetext;

}

lv.load("test.txt");

trace(lv); // "undefined"
trace("-");
trace(var_text_string); // just nothing

Thanks again for your great help !
 
you are tracing at the wrong point

var lv:LoadVars = new LoadVars();

lv.onData = function(thetext:String) {

if (thetext != undefined) {
trace(thetext)
} else {
trace("Unable to load external file.");
}

}

lv.load("test.txt")
 
Thanks Bill :)

But even this returns absolutely nothing :

Code:
var lv:LoadVars = new LoadVars();

lv.onData = function(thetext:String) {

trace("ok"); // returns nothing

if (thetext != undefined) {
    
    trace(thetext); // returns nothing
    
    } else {
    
    trace("Unable to load external file."); // returns nothing
    
    }

}

lv.load("test.txt")

Could it be because I'm using Flash MX 6.0 (2002 version) ?
 
ok for mx

var lv= new LoadVars();

lv.onLoad = function() {

trace("ok");

if (this.thetext != undefined) {

trace(this.thetext);

} else {

trace("Unable to load external file."); // returns nothing

}

}

lv.load("test.txt")


and change the text file to

&thetext=a|1|2|3
b|4|5|6
c|7|8|9&

that will load it and then you can apply earlier code to get the data into arrays



 
Thanks Bill but I need to avoid having the variable name inside the text file. Is it impossible with the version of Flash I'm using?

Thanks again :)
 
ok with nothing in the text file except data

_lv = new LoadVars();
_lv.onLoad = function() {
_tempArray = unescape(this).split("=");
trace(_tempArray[0])
_tempArray2 = _tempArray[0].split("|");
};
_lv.load("test.txt");
 
[Code Almost What you want]

_lv = new LoadVars();
_lv.onLoad = function() {
_tempArray = unescape(this).split("=");
_tempArray2 = escape(_tempArray[0]).split("%0D%0A");
for (a=0; a<_tempArray2.length; a++) {
this["arr"+a] = []
this["arr"+a].push(unescape(_tempArray2[a]).split("|"))
}
trace(this.arr2) //returns c,7,8,9
trace(this.arr0[0][0]) //returns a
trace(this.arr1[0][2] )//returns 5
};
_lv.load("test.txt");
[/code]
 
Many thanks, Bill ;)

But, after testing just this ...

Code:
_lv = new LoadVars();
_lv.onLoad = function() {

trace(this);

};

_lv.load("test.txt");

... I've noticed that all the special characters are encoded, which I would like to avoid.

Does Flash allow the importation of text files just "as is", without any modification whatsoever?

Again, your help has been really great.

Cheers :)
 
Ok, I was afraid that escape/unescape would pose problem with foreign character sets like Japanese, but I didn't, as it seems.

So, here is the final code :

Code:
_lv = new LoadVars();
_lv.onLoad = function() {

// clean up text sting
var_temp_array = unescape(this).split("=&onLoad=");
var_temp_string = var_temp_array[0].split('\r\n').join('\r');

// make array
var_temp_array = var_temp_string.split('\r');

    for (a=0; a < var_temp_array.length; a++) {
    
    var_last_array.push(var_temp_array[a].split('|'));

    }

}

_lv.load("test.txt");

Have a great day, Bill ;)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top