views:

355

answers:

3

HI,

I am reading the text file

1=apple 2=jack 3=lemon 4=banana

    var loader:URLLoader = URLLoader(event.target);
var mystring :String = loader.data;
 tempArray = mystring.split("\n");

and getting the value like

1=apple

2=jack

3=lemon

4=banana

i need to split the value and push in to array like..removing the "=" and "end space "

"1=apple " split that value in to 1 and apple. "2=jack " split that value in to 2 and jack.

and push in to new array called fruits array using the 1 2 3... as index;

fruits[1]="apple"; fruits[2]="jack";lemon fruits[3]="lemon";

thanks in advance

A: 

Did you try this instead of splitting into newline,

tempArray = mystring.split(" ");

...and the dealing with each element in your array separately to strip anything before the "=" sign for each element.

A: 

Try this:

var lines:Array /* of String */ = String(loader.data).split(/ *\n */);
var fruits:Array = [];
for each (var line:String in lines) {
    var tokens:Array /* of String */ = line.split('=');
    fruits[int(tokens[0])] = tokens[1];
}
David Hanak
+1  A: 

I know that this might be seen as an inadequate answer but:

Why don't you use XML? XML-files are pretty easy to read in AS and they always come with a structure that simple textfiles can hardly provide...

As an example:

<fruits>
    <fruit index="1" name="Apple" />
    <fruit index="2" name="Jack" />
    <fruit index="3" name="Lemon" />
    <fruit index="4" name="Banana" />
</fruits>

And the AS would be something like:

var fruits:Array = new Array();
var xml:XML = new XML();
xml.ignoreWhite = true;
xml.onLoad = function()
{
    var nodes:Array = this.firstChild.childNodes;
    for(var i=0; i<nodes.length; i++)
        fruits.push(nodes[i].name);        // *
}
xml.load(xmlFile);

The line with the * can be replaced by something like fruits[nodes[i].index] = nodes[i].name if you insist on using the indices from the file.

Kevin D.