PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
How to populate arrays from a text file?
A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: How to populate arrays from a text file?

  1. #1
    Senior Member
    Join Date
    Aug 2000
    Location
    Central PA
    Posts
    120

    How to populate arrays from a text file?

    I have a text file with about 1000 rows of data and about 100 values (separated by spaces) in each row. What I need to do is to load the data into Flash into a separate array for each row of data, but am not sure how to get started. I will no doubt have to name each array dynamically as I don't want to hand code 1000 names for arrays! Any help appreciated.
    ?:-{>

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    First, I'd suggest formatting your input file in some sort of standard format. XML is very popular, but probably overkill if your structure is a simple 2d array. CSV is a good standard format for simple spreadsheet like data. CSV parsing is not built in, but there is a library for it: http://code.google.com/p/csvlib/

    If you can't do either of those, then just take your whole text as a String, and use the split function to break it into rows (split on "\n"). Then iterate through each of those rows and split each on " ". Do NOT create a new variable for each row. Instead, just create a 2d array. That is, an array in which each entry is also an array. The inner arrays will have your actual values as entries.

  3. #3
    Senior Member
    Join Date
    Aug 2000
    Location
    Central PA
    Posts
    120
    That makes sense. I will play around with that idea. Thanks, 5TonsOfFlax.
    ?:-{>

  4. #4
    Senior Member
    Join Date
    Aug 2000
    Location
    Central PA
    Posts
    120
    Okay, here is what I came up with and it seems to load the data and I can trace the first value of the first row, however, Flash times out after 15 seconds if I have more than say 120 rows being read in.

    Actionscript Code:
    var numOfRows:int = 120;  
    var mainArr:Array = new Array(numOfRows);
    var rowArr:Array = new Array();
    var currentArr:Array = new Array();
    var i:int;  
    var j:int;  

    var myLoader:URLLoader = new URLLoader()
    myLoader.load(new URLRequest("pulsar.txt"))
    myLoader.addEventListener(Event.COMPLETE, parseMyData)

    var myText = myLoader.data;

    function parseMyData(ev:Event) {
       
    for (i = 0; i < numOfRows; i++) {  
        mainArr[i] = new Array(numOfRows);  
        for (j = 0; j < numOfRows; j++) {  
            rowArr = ev.target.data.split("\n");
            mainArr[i][j] = "[" + i + "][" + rowArr[j] + "]";  
           
        }  
    }  
    currentArr = rowArr[0].split(" ");
    trace(currentArr[0]);
    }
    ?:-{>

  5. #5
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You've got a lot of stuff in there that doesn't make much sense. You're setting the myText variable before the loader has completed, and then never using it. You're using the numberOfRows variable to also mean number of columns. And you don't need either.
    You're re-splitting the entire file for every element in every row. And the value you're putting into the final array is a constructed string that looks like "[1][whatever]" (where the 1 will change according to the row number).

    Code:
    var mainArr:Array = [];
    
    var myLoader:URLLoader = new URLLoader();
    myLoader.load(new URLRequest("pulsar.txt"));
    myLoader.addEventListener(Event.COMPLETE, parseMyData);
    
    function parseMyData(e:Event):void{
      var myText:String = e.target.data;
      var rows:Array = myText.split("\n");
      for (var i:int = 0; i < rows.length; i++){
        mainArr.push(rows[i].split(" "));
      }
    }

  6. #6
    Senior Member
    Join Date
    Aug 2000
    Location
    Central PA
    Posts
    120
    wow, you really cut that down pretty good and it loads quickly now. I am very much obliged, 5Tons!
    ?:-{>

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center