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 parsing

Status
Not open for further replies.

xavstone

Technical User
Apr 12, 2005
33
GB
Hi all,

I need to parse text in a file into an array. The file type im trying to read is an .obj file. In c# i would just create streamreader object and get parsing. But new to actionscript(v8). Obj files store information like this:

v 123 145 234
v 234 245 242
...

where v indicates the variable type and the values after indicate the array elements. Specifically what i need to do is read each line of the file, determine whether it starts with a certain character, then load the values after the character into an array. Loadvars() seems to handle name value pairs only.

If anyone can help it would be much appreciated...spent too much time on this one...

Thanks, Ben

 
Just to get you started

_lv = new LoadVars()
_lv.onLoad = function(){
bert = this.toString()
bob = bert.split("=")
bill = bob[0].split("%0D%0A")
for(i=0;i<bill.length;i++){
trace(unescape(bill))
}
}
_lv.load("test.obj")

This will trace each line of the obj file. You could then further split each element of bill to determine the first letter.

The above code is very sloppy but its Sat afternoon and ill leave you to tidy it up yourself.
 
thanks for that bill, i did this with it:

import idi.*;
class idi.trimesh {
//Mesh data
public var vCount:Number;
public var vList = new Array();
public var tCount:Number;
public var tList = new Array();

public var intext:String;

public function trimesh() {
vList.Array = null;
tList.Array = null;
}

public function buildObj(filename:String) {

var bob:Array;
var bert:Array;

var importObj:LoadVars = new LoadVars();

importObj.onData = function(src:String) {

intext = src;
intext = intext.split(" ").join(" ");
bob = intext.split("\r\n");

//set loop counter
var i:Number;
for (i = 0; i < bob.length; i++) {
bert = bob.split(" ");
if (bert[0] == "v") {
var tempvert:vertex = new vertex();
trace(bert[1]);
tempvert.pos.px = bert[1]; trace(tempvert.pos.px);
tempvert.pos.py = bert[2]; trace(tempvert.pos.py);// THESE JUST RETURN AS UNDEFINED
tempvert.pos.pz = bert[3]; trace(tempvert.pos.pz);

vList[vList.length] = tempvert;
}
}
}
importObj.load(filename);
}
}

But i everytime i try and set the public vars of this class they just return as undefined. I have been getting this alot so i think i must be doing something wrong...globally. Did i mention i was new to actionscript :/....

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top