www.webdeveloper.com
Results 1 to 10 of 10

Thread: Convert array to separate floats

  1. #1
    Join Date
    Feb 2014
    Posts
    5

    Convert array to separate floats

    Hello

    I am struggling with JS code. I am using it in conjunction with MaxMSP. Basically whay I do is load a csv file that gets converted to be stored in a coll object (in Max). What I understand from the code is:
    1) it reads the csv file line by line and splits the string into arrays
    2) it outputs the array.

    What is wrong:
    1) I need an index number before every line.
    2) I want the values as floats (except the date which can be a symbol)
    3) I need to get rid of the first line which contains the variables names
    4) Finally the lines should be reversed so what is at the last line in the csv comes first

    3 and 4 I can do in Max itself but 1 and 2 I cannot. I can read the JS code and I understand what it does but I do not know how to take it further. I hope somebody here can help me. Here goes the JS code:

    Code:
    function import(filename)
    {
    	var f = new File(filename);
    	
    	if (f.open) {
    		outlet(0, "clear");
    		
    		while (f.position < f.eof) {
    		
    			var str = f.readline();	
    			var a = str.split(","); // conver strings to array (elements are delimited by a coma)
    			a[5] /= 1000; // uncomment to devide the 6th column by 1000
    	
    
    
    			outlet(0, "store", a);	// store in the coll
    		}
    		f.close();
    	} else {
    		error("couldn't find the file ("+ filename +")\n");
    	}
    }

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,440
    Can you provide a sample CSV text file (10 lines or so)
    AND
    the results of what the conversion (as test strings, not floats) should look like for testing purposes?
    Should then take only one small change to convert the test strings to float values.

  3. #3
    Join Date
    Feb 2014
    Posts
    5
    Hello

    I download data from Yahoo Finance as a csv file. They contain 7 columns (date, open, high, low, close, volume, adjusted close).
    1) The first line is a header which contains the names of the columns. I need to get rid of that.
    2) Then, as the data are put in reverse chronological order, I need to reverse the file so the earliest date comes up at the top.
    3) Then, I need to have the index number in front of each line. Max can only use the dataset if there is an index number in front of it.
    4) the 6th column should be divided by 1000 to make high numbers fall within reasonable limits. I can do this is Max (this calculation is not in the attached file).

    In the links you can find the dataset as a csv file and the output I need as a text file (Max needs a txtfile).

    https://www.dropbox.com/s/fe8nx1bcqe...shorttable.csv
    https://www.dropbox.com/s/r5i09sxn43...rsedataset.txt

  4. #4
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,440

    Lightbulb

    This version uses your local directory,
    but you can convert it easily to an AJAX solution if the information is on the server.
    Code:
    <!DOCTYPE html>
    <html>
    <meta charset="UTF-8">
    <head>
    <title>Reading Local File</title>
    </head>
    <body>
    <!-- <input type="file" onchange='readText(this);document.getElementById('main').innerHTML=rawData' /> -->
    <input type="file" onchange='readText(this)' />
    <pre id="main"></pre>
    <p>
    <button onclick="convert()">Convert</button>
    <pre id="debug"></pre>
    
    <script type="text/javascript">
      var reader = new FileReader();
    
      function readText(that){
        if (that.files && that.files[0]) {
          var reader = new FileReader();
          reader.onload = function (e) {  
    	var output=e.target.result;
    // read all of text file
    	output=output.split("\n").filter(/./.test, /./).join("\n");
    	document.getElementById('main').innerHTML= output;
    
          }; // end onload()
          reader.readAsText(that.files[0]);
        } // end if html5 filelist support
      } 
      
      function convert() {
        var csv = [], tarr = [], maxTxt = [], str = '';
        txt = document.getElementById('main').innerHTML;  // alert(txt);
    	csv = txt.split(/\n/);
        csv = csv.reverse();
        csv.pop();  // drop last (first line heading)
    	for (var i=0; i<csv.length; i++) {
    	  tarr = csv[i].split(',');
    	  tarr.unshift((i+1)+',');
    	  tarr[6] = (Number(tarr[6])/1000).toFixed(0);
    	  maxTxt.push(tarr.join(' '));
    	}
    	str = maxTxt.join(';<br>')+';';
        document.getElementById('debug').innerHTML = str;	
      }
    </script>
    </body>
    </html>
    Good Luck!

  5. #5
    Join Date
    Feb 2014
    Posts
    5
    Hello

    I am afraid I do not understand your post. I have the csv files already. The js code happens within MaxMSP, nothing happens in a browser or so. I managed to get an index number (just i++ did the trick), now I need to get the rest in order. I suppose I have to look at this part of your code right?
    =======
    function convert() {
    var csv = [], tarr = [], maxTxt = [], str = '';
    txt = document.getElementById('main').innerHTML; // alert(txt);
    csv = txt.split(/\n/);
    csv = csv.reverse();
    csv.pop(); // drop last (first line heading)
    for (var i=0; i<csv.length; i++) {
    tarr = csv[i].split(',');
    tarr.unshift((i+1)+',');
    tarr[6] = (Number(tarr[6])/1000).toFixed(0);
    maxTxt.push(tarr.join(' '));
    }
    ========

    I will try to find out how to make it work and post again if I need more help (which will probably be the case).

  6. #6
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,440

    Arrow

    Quote Originally Posted by Thinksamuel View Post
    Hello

    I am afraid I do not understand your post. I have the csv files already. The js code happens within MaxMSP, nothing happens in a browser or so. I managed to get an index number (just i++ did the trick), now I need to get the rest in order. I suppose I have to look at this part of your code right?
    Code:
    ...
    I will try to find out how to make it work and post again if I need more help (which will probably be the case).
    While you may have the csv files already, I do not, so I used the sample you posted. Obviously use the files you have.

    I provided a function for the browser because I do not have or use MaxMSP.
    I have no idea what it does or where to get it and you did not provide it.

    If your needs require a function to convert the csv information into another text file, then the convert() should do it.
    That's all I did. Your initial request asked to translate the information from csv to text in a particular format.
    That's what you got.

  7. #7
    Join Date
    Feb 2014
    Posts
    5
    Hello

    I am going to need some help. I managed mostly what I wanted:
    1) get rid of header
    2) convert to float
    3) get an index number
    But I am still struggling with having the lines read backwards. They still come out antichronologically. I have tried your code but I think I am doing it wrong. I tried other methods but they as well fail to do the trick. I parsed the date as a float and tried to sort but I cannot get the syntax right.

    I uploaded a testapp where you can see what I am doing. You basically drop the csvfile on top of the hotspot and it is sent through the js object. If you then doubleclick the coll object, you can see the contents.

  8. #8
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,440

    Question

    Quote Originally Posted by Thinksamuel View Post
    Hello

    I am going to need some help. I managed mostly what I wanted:
    1) get rid of header
    2) convert to float
    3) get an index number
    But I am still struggling with having the lines read backwards. They still come out antichronologically. I have tried your code but I think I am doing it wrong. I tried other methods but they as well fail to do the trick. I parsed the date as a float and tried to sort but I cannot get the syntax right.

    I uploaded a testapp where you can see what I am doing. You basically drop the csvfile on top of the hotspot and it is sent through the js object. If you then doubleclick the coll object, you can see the contents.
    Your example 'testapp' and csv file do not download for me. Difficult to comment upon them.
    Post your script between [ code] and [ /code] tags (without the spaces) unless it is just too big to upload.

    The reversal of the csv records occurs in this section:
    Code:
    csv = txt.split(/\n/);
    csv = csv.reverse();
    csv.pop(); // drop last (first line heading)
    I just process the contents of the file in reverse order to begin with.

    If you tried my code and it did not work, show what you modified as it works fine as I sent it.
    It is difficult for me to comment on code that I cannot see.

  9. #9
    Join Date
    Feb 2014
    Posts
    5
    Here you go. Here is a new link to the app and the csv
    Code:
    function import(filename)
    {
        var f = new File(filename); 
        if (f.open) {
            var i = 0;
            outlet(0, "clear");
            f.readline();
    	    while (f.position < f.eof)
            {
                var str = f.readline();	
               var a = str.split(","); // convert strings to array (elements are delimited by a coma)
                // a[5] /= 1000; // uncomment to divide the 6th column by 1000		
    			var date = Date.parse(a[0]);
        		var date = parseFloat(date);
                var open = parseFloat(a[1]);
                var high = parseFloat(a[2]);
                var low = parseFloat(a[3]);
                var close = parseFloat(a[4]);
                var volume = parseFloat(a[5]);
                var adjusted_close = parseFloat(a[6]);        
            }
        outlet(0, i++, date,open,high,low,close,volume,adjusted_close); // store in the coll  
            f.close();
        } else {
            error("couldn't find the file ("+ filename +")\n");
        }
    }

  10. #10
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,440
    I don't see where the function outlet is defined.
    Can you push the output of that function to a temporary array,
    and then reverse the write option to the storage container?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles