Home | Flash AS3 Tutorials | Flash Animation Tutorials | Store

Flash Text Database Tutorial: Split Database into Array

The text database has been loaded into a Flash Movie in previous Flash ActionScript tutorial. It's time to see how to split the database into an Array.

Please update flash player to view this Flash ActionScript tutorial!

Flash Tutorial Content:

Split Method:
Use split method to split a String into substrings by breaking it where the Item Delimiter occurs, and returns the substrings in an array.

Example:
myString.split("Item Delimiter")

The complete Flash Movie is shown as above, you may try how it works before you start this tutorial.

Flash ActionScript Codes:

//You can declare the url of the text file as string
//so that you can use it later
var url:String = "staff.txt";

//Create a URLLOader object with the name myLoader
var myLoader:URLLoader = new URLLoader();

//Load the text file by using URLRequest
//Can be local file or URL of webpage
myLoader.load(new URLRequest(url));

//Listen when the loading of text file COMPLETE
//Call the loadComplete function when the loading COMPLETE
myLoader.addEventListener(Event.COMPLETE, loadComplete);

//Declare some variables to use later
var myString:String;

function loadComplete(evt:Event) {

//Assign the loaded file into a string (myString)
myString = evt.target.data;

//Remove the line carriage return
//i.e.
//Split the myString into an Array
//Join the elements of the Array without space
// \r is the line carriage return
myString = myString.split("\r").join("");

//Display the file content
output_txt.text = myString;

//Remove Listener that no longer in use
myLoader.removeEventListener(Event.COMPLETE, loadComplete);

}

 

//////////////////////////////
///// ERROR HANDLING /////
/////////////////////////////

 

// Listen error events for the loading process
// Call loadError function in case an error appear
myLoader.addEventListener(IOErrorEvent.IO_ERROR, loadError1)
myLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, loadError1)
myLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, loadError2)

function loadError1(event:ErrorEvent):void {
// Display error message to user in case of loading error.
output_txt.text = "Sorry that there is an error during the loading of an external text file. The error is:" + "\n" + event.text;
}

function loadError2(event:HTTPStatusEvent):void {
// Display error message to user in case of loading error.
output_txt.text = "Sorry that there may be an error during the loading of an external text file. The error is:" + "\n" + event.status;
}

Download Flash Source File:

Flash Source File text-database-2.fla

Remarks:

Ah... The blank lines between the records are line carriage return. Now there are only line feed (\n) between the records. In other words, there are line carriage return (\r) and line feed (\n) between the records. Therefore we can use line carriage return and line feed (i.e. \r\n) as the Item delimiter and split the text database into an Array where each element hold a record.

Still not understand? Everything will be clear when you read all the Flash Actionscript text database tutorials.