0

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

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.