Reading a text file and splitting a string and clicking on an array

HI,

I am reading a text file

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

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

      

and get a value like

1 = apple

2 = connector

3 = lemon

4 = banana

I need to split the value and insert into an array, for example break "=" and "trailing space"

"1 = apple" divides this value by 1 and apple. "2 = slot" splits this value into 2 and slot.

and click on a new array called fruit array using index 1 2 3 ... as index;

fruit [1] = "apple"; fruit [2] = "jack", lemon fruit [3] = "lemon";

early

0


a source to share


3 answers


I know this can be seen as an inadequate answer, but:

Why aren't you using XML? XML files are fairly easy to read in AS, and they always have a structure that simple text files 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 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 * can be replaced with something like fruit [nodes [i] .index] = nodes [i] .name if you insist on using the indices from the file.

+1


a source


Have you tried this instead of splitting to a new line,

tempArray = mystring.split(" ");

      



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

0


a source


Try the following:

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

      

0


a source







All Articles