/    Sign up×
Community /Pin to ProfileBookmark

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”);
}
}
[/CODE]

to post a comment
JavaScript

9 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERFeb 21.2014 — 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.
Copy linkTweet thisAlerts:
@ThinksamuelauthorFeb 22.2014 — 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/fe8nx1bcqe2k19r/shorttable.csv

https://www.dropbox.com/s/r5i09sxn436c1hv/reversedataset.txt
Copy linkTweet thisAlerts:
@JMRKERFeb 22.2014 — This version uses your local directory,

but you can convert it easily to an AJAX solution if the information is on the server.
<i>
</i>&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;meta charset="UTF-8"&gt;
&lt;head&gt;
&lt;title&gt;Reading Local File&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;!-- &lt;input type="file" onchange='readText(this);document.getElementById('main').innerHTML=rawData' /&gt; --&gt;
&lt;input type="file" onchange='readText(this)' /&gt;
&lt;pre id="main"&gt;&lt;/pre&gt;
&lt;p&gt;
&lt;button onclick="convert()"&gt;Convert&lt;/button&gt;
&lt;pre id="debug"&gt;&lt;/pre&gt;

&lt;script type="text/javascript"&gt;
var reader = new FileReader();

function readText(that){
if (that.files &amp;&amp; that.files[0]) {
var reader = new FileReader();
reader.onload = function (e) { <br/>
var output=e.target.result;
// read all of text file
output=output.split("n").filter(/./.test, /./).join("n");
document.getElementById('main').innerHTML= output;

<i> </i> }; // end onload()
<i> </i> reader.readAsText(that.files[0]);
<i> </i>} // 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&lt;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(';&lt;br&gt;')+';';
document.getElementById('debug').innerHTML = str;
}
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

Good Luck!

?
Copy linkTweet thisAlerts:
@ThinksamuelauthorFeb 22.2014 — 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).
Copy linkTweet thisAlerts:
@JMRKERFeb 22.2014 — 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?
<i>
</i>...

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).[/QUOTE]


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.
Copy linkTweet thisAlerts:
@ThinksamuelauthorFeb 25.2014 — 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.
Copy linkTweet thisAlerts:
@JMRKERFeb 25.2014 — 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.[/QUOTE]


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:
<i>
</i>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.
Copy linkTweet thisAlerts:
@ThinksamuelauthorFeb 25.2014 — 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");
}
}[/CODE]
Copy linkTweet thisAlerts:
@JMRKERFeb 25.2014 — 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?
×

Success!

Help @Thinksamuel spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...