Announcement

Collapse
No announcement yet.

Load Data [CSV/TXT] into 2 Dim Array

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Load Data [CSV/TXT] into 2 Dim Array

    Hello,

    I have a few text files that I would like to load into an array. I know how to load 1 dimensional data into an array, but I do not know how to add multidimensional data into an array. I suppose I need to parse the lines of data into elements somehow and load each line as an array to the existing array, but I do not know how to do that.

    My text file stores 5 elements per line separated by commas (',') and looks like this:

    Q,100,Support,20090101,50
    Q,119,Support,20010203,49

    I would like the array structured in 2 dimensions, so I could reference the items by row and column.

    PHP Code:
    var aArray = new Array[]; 

    function 
    preMain() { 
       
    loadData(); 


    function 
    loadData() { 
    var 
    = new File(MyFile.txt); 
    var 
    0

    if( 
    f.exists() ) {
        
    f.open("rt");
            while( !
    f.eof()) {
                
    aArray[i] = f.readln();
                
    i++;
                }
            }

       
    //debugPrintln(aArray); 

       
    return; 

    Last edited by novacoda; 03-23-2016, 10:17 PM.

  • #2
    Re: Load Data [CSV/TXT] into 2 Dim Array

    Here is a solution.

    PHP Code:
    var aArray = new Array[];  
    var 
    bArray = new Array[];  

    function 
    preMain() {  
       
    loadData();  
    }  

    function 
    loadData() {  
    var 
    = new File(MyFile.txt);  
    var 
    0;  

    if( 
    f.exists() ) { 
        
    f.open("rt"); 
            while( !
    f.eof()) { 
                
    aArray[i] = f.readln(); 
                
    i++; 
                } 
            } 

            for (
    0aArray.lengthi++){
                var 
    allElementsIntervalPriceRSdateIndexfiveElements;
                
    allElements aArray[i].split(',');
                
    Interval allElements[0];
                
    Price allElements[1];
                
    RS allElements[2];
                
    date allElements[3];
                
    Index allElements[4];
                
    fiveElements = [Interval,Price,RS,date,Index];
                
    bArray.push(fiveElements);
            }

       
    //debugPrintln(bArray);  

       
    return;  

    Comment

    Working...
    X