Skip to main content
Known Participant
February 2, 2014
Answered

dynamically read data from a txt document

  • February 2, 2014
  • 6 replies
  • 1458 views

I try to dynamically read data from a txt document

in swf

stop ();

var pafh=this;

import flash.events.*;

var Araray_id:Array =new Array();

var v_length:Number;

var myTextLoader:URLLoader = new URLLoader();

myTextLoader.dataFormat=URLLoaderDataFormat.VARIABLES;

myTextLoader.addEventListener(Event.COMPLETE, onLoaded);

function onLoaded(e:Event):void {

        Araray_id = e.target.data.araray_id.split(",");

        trace("Araray_id: "+Araray_id);

        v_length=Araray_id.length;

        trace("v_length: "+v_length);

        for (var i:Number=0;i<v_length;i++){

            pafh["Id_"+i]=new Array();

            trace("pafh[Id_+i]: "+pafh["Id_"+i]);

            var v_help:Object="id_"+i;

            trace("v_help: "+v_help);

            //pafh["Id_"+i]= e.target.data.v_help.split(",");????? Here stops the script

            //pafh["Id_"+i]= e.target.data.(v_help).split(",");????Here stops the script

            //pafh["Id_"+i]= e.target.data.[v_help].split(","); ????Here stops the script

           

            }

        play();

}

myTextLoader.load(new URLRequest("myText1.txt"));

-------------------------------------------------------

in text

araray_id=aa,bb,cc,dd,ee,ff

&id_0=aa1,aa2,aa3,aa4

&id_1=bb1,bb2,bb3,bb4,bb5

&id_2=cc1,cc2,cc3

&id_3=dd1,dd2,dd3,dd4,dd5,dd6

&id_4=ee1,ee2

&id_5=ff1,ff2

-------------------------------------------------------

output

Araray_id: aa,bb,cc,dd,ee,ff

v_length: 6

pafh[Id_+i]:

v_help: id_0

TypeError: Error #1010: A term is undefined and has no properties.

    at skuskaceatarraybiforas3fromtext_fla::MainTimeline/onLoaded()

    at flash.events::EventDispatcher/dispatchEventFunction()

    at flash.events::EventDispatcher/dispatchEvent()

    at flash.net::URLLoader/onComplete()

Can you help me with this problem

This topic has been closed for replies.
Correct answer Ned Murphy

            //pafh["Id_"+i]= e.target.data.v_help.split(",");????? Here stops the script

            //pafh["Id_"+i]= e.target.data.(v_help).split(",");????Here stops the script

            //pafh["Id_"+i]= e.target.data.[v_help].split(","); ????Here stops the script

               pafh["Id_"+i]= e.target.data.???????.split(",");


You had those lnes commented out so I did not consider them as being in what you ran to get that error.

Try... 

pafh["Id_"+i]= e.target.data[v_help].split(",");

(note the dot removed after data)

and create v_help as a String, not an Object

6 replies

Inspiring
February 2, 2014

More suggestions.

1. If you align naming conventions between data and your application - reading data could be practically a one-liner.

If you write pafh["id_" + i] (with small case i) instead of pafh["Id_" + i] (with capital case I) the function can be just:

function onLoaded(e:Event):void

{

          for (var prop:String in e.target.data)

          {

                    pafh[prop] = e.target.data[prop].split(",");

          }

          play();

}

2. If changing conventions is not feasible, here is another thing you can do:

function onLoaded(e:Event):void

{

          for (var prop:String in e.target.data)

          {

                    pafh[prop.replace(/^\w/, String(prop.match(/^\w/)).toUpperCase())] = e.target.data[prop].split(",");

          }

          play();

}

Inspiring
February 2, 2014

And the last thing.

Function onLoaded has several overkills. This one accomplishes the same thing with much less code and it is much faster:

function onLoaded(e:Event):void

{

          var data:Object = e.target.data;

          var v_length:int = data.araray_id.split(",").length;

          for (var i:int = 0; i < v_length; i++)

          {

                    pafh["Id_" + i] = data["id_" + i].split(",");

          }

          play();

}

Inspiring
February 2, 2014

It looks like the main reason for the error is variable reading syntax:

You have dot:

pafh["Id_"+i]= e.target.data.[v_help].split(",");

It must be (no dot after data):

pafh["Id_"+i]= e.target.data[v_help].split(",");

Inspiring
February 2, 2014

You attempt to read variables that do not exist in the data:

e.target.data.v_help.split(",");

Data has id_0, id_1, etc.

In any case, I must side with Anton, providing data via url variables is not the best approach to say the least. XML is a much better choice.

Known Participant
February 3, 2014

Maybe you're right with XML is good,
but I make up DYNAMIC websites, and I think it is easier for an amateur rewrite the text document as an XML document.

it works correctly

pafh["Id_"+i]= e.target.data[v_help].split(",");

thank you all


Ned Murphy
Legend
February 2, 2014

Go into your Flash Publish settings and select the option to Permit Debugging.  The error message might now include a line number that will point to the line where the problem arises.

Known Participant
February 2, 2014

I know which line is the problem. we need to resolve the problem.
but thank you taught me something new

Known Participant
February 2, 2014

            //pafh["Id_"+i]= e.target.data.v_help.split(",");????? Here stops the script

            //pafh["Id_"+i]= e.target.data.(v_help).split(",");????Here stops the script

            //pafh["Id_"+i]= e.target.data.[v_help].split(","); ????Here stops the script

               pafh["Id_"+i]= e.target.data.???????.split(",");

User Unknow
Legend
February 2, 2014

why not to use xml for this??