It happened recently that I needed to load a list of items from a text file. There was no other structure to the file except that each item in the list was on a separate line. The items were not in XML and they were not in form variables format (used by LoadVars).

The solution to this is to use the onData event of the LoadVars object. The onData is triggered before the onLoad and is passed the raw contents of the file in its parameter. From this raw content we can extract the list as an array.

I wrapped the solution up in a class as follows.

class ListLoader extends LoadVars
{
    public var data:Array;
    
    public function onData( src:String ):Void
    {
        if( src == undefined )
        {
            onLoad( false );
        }
        else
        {
            // check for different line endings
            if( src.indexOf( "\r\n" ) != -1 )
            {
                data = src.split( "\r\n" );
            }
            else if( src.indexOf( "\n" ) != -1 )
            {
                data = src.split( "\n" );
            }
            else if( src.indexOf( "\r" ) != -1 )
            {
                data = src.split( "\r" );
            }
            else // no line endings - only one item in the list
            {
                data = new Array();
                data.push( src );
            }
            onLoad( true );
        }
    }
}

This can be used just like a LoadVars object, except that the loaded data is in the data property of the ListLoader. This data is an Array. So you use it like this.

var loader:ListLoader = new ListLoader();
loader.onLoad = function( success:Boolean ):Void
{
    if( success )
    {
        // use list, which is loader.data
        trace( loader.data );
    }
    else
    {
        // error
        trace( "error" );
    }
}
loader.load(  "somelist.txt" );

I find this useful for client configurable options since many people don’t understand XML or form variable strings, but everyone understands “put each item on a separate line”.