SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Oct 2008
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Parsing through a txt file with Flash

    Hey,

    I'm trying to create a dynamic list of inputs from a text file but I'm having trouble ignoring undefined elements. Here's the code:

    Code Actionscript:
    myData = new LoadVars() 
            myData.load("test.txt") 
            myData.ref = this 
            myData.onLoad = function(succes){ 
                if(succes){ 			
                    for(var i=0; i<20; i++){
    					this.ref["id_"+i].html = true;
     
    					if (this.ref["id_"+i] != 0 )
    					{				
                        this.ref["id_"+i].htmlText = "<b>"+this["id_"+i]+"</b>" 					
    					}
    					else if (this.ref["id_"+i] == undefined)
    					{
    						this.ref["id_"+i].htmlText = "<b>"+0+"</b>"						
    					}					
                    } 				
                } else trace("Error loading data");			
            }

    and the txt file is as below:

    Code:
    id_1=01234&id_2=12345&id_4=333&id_8=123123
    The problem is that as the txt file doesn't have an element with "id_3", its value is set to be "undefined". How to change it so that if the txt file does not contain an id, it's value is set to be "0"?

  2. #2
    SitePoint Member
    Join Date
    Oct 2008
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I found an easier way to get the data I needed:
    Code Actionscript:
    for(i in this){
    	try{
    		this.ref[i].html = true;
    		this.ref[i].htmlText = "<B>"+this[i]+"</B>";
    	}
    	catch(e){							
    	}

    Now the code works in a different way. It checks for matching entries in the text file and the flash file and updates only their values. If a dynamic text field in the flash doesn't exist in the text file, it won't be displayed at all.

    Still trying to figure out how to show "0" on the non-existent text fields though...

  3. #3
    SitePoint Member
    Join Date
    Oct 2008
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks so much for everybody's help, you've been very nice so far. A friend figured it out for me: using

    for(vObjectName in this.ref) {
    ...

    Allows me to do everything I need. I'll just add an if clause

    if (vObjectName.subsrt(0,3) == "id_"){
    ...

    I can get the strings in the text that have "id_" in their beginning. Now I'll just need to distinguish the strings in the text file that have a dynamic text field in flash, from the ones that do not have. By using

    if(isNaN(this[vObjectName])){

    I get the dynamic text fields that don't have a match in the text file, so I can assign their value to be 0. Then using else statement put the value of the text file object to the flash object. Easy.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •