cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Did you get an answer that solved your problem? Please mark it as an Accepted Solution so others with the same problem can find the answer easily. X

how to sum values

Gucio
14-Alexandrite

how to sum values

hi all,

 

I would like to read values from csv file, and count sum of all values in each column.

please look at code below and give me answer - why commented row is wrong?? 

(The goal is to create dynamic dataShape with dynamic names of its column.) 

 

var fileRepository = Things["SystemRepository"];
var fileContent = fileRepository.LoadText({
path: "suma.csv" }
);
var rows = fileContent.split("\n");

var header = rows[0].split(",");
var columnsQty = header.length;
var myInfoTable = { dataShape: {fieldDefinitions : { } }, rows: [] };
var abc = 0;
var header_Sum;

for ( var i=0; i<2; i++) {
header_Sum = "Sum_" + i;

myInfoTable.dataShape.fieldDefinitions[header_Sum]={
name: header_Sum,
baseType: "NUMBER"
}
abc = abc + parseFloat(rows[i]);
};

//myInfoTable.rows.push({header_Sum:abc});

myInfoTable.rows.push({Sum_0:abc});
result = myInfoTable;

 

thx 

gucio

Krzysztof
1 ACCEPTED SOLUTION

Accepted Solutions

Build the row object before:

var newRow = {};
newRow[""+header_Sum] = abc;
myInfoTable.rows.push(newRow);

 

View solution in original post

7 REPLIES 7

Better you got with Infotable object instead of JSON infotable.

 

 

var fileRepository = Things["SystemRepository"];
var fileContent = fileRepository.LoadText({
path: "suma.csv" }
);
var rows = fileContent.split("\n");

var header = rows[0].split(",");
var columnsQty = header.length;
/*var myInfoTable = { dataShape: {fieldDefinitions : { } }, rows: [] };
*/
// -- Instead you should use the corresponding snippet:
var myInfoTable = Resources["InfoTableFunctions"].CreateInfoTable();

var abc = 0; var header_Sum; for ( var i=0; i<2; i++) { header_Sum = "Sum_" + i; /* myInfoTable.dataShape.fieldDefinitions[header_Sum]={ name: header_Sum, baseType: "NUMBER" }
*/
// -- Instead you should use the corresponding snippet
myInfoTable.AddField({ name: header_Sum, baseType: "NUMBER" }); abc = abc + parseFloat(rows[i]); }; //myInfoTable.rows.push({header_Sum:abc}); /* myInfoTable.rows.push({Sum_0:abc});
*/
// -- Instead you should use the corresponding snippet
myInfoTable.AddRow({ "Sum_0": abc }); result = myInfoTable;

But on your code I don't see it iterating over rows, it's right? You are iterating, if I understand well, over columns (2 columns) but not rows.

 

Gucio
14-Alexandrite
(To:CarlesColl)

hi,

 

My code was written just to ask how to do it. I couldn't find solution why infoTable was not populated with values when I used "dynamic" names of headers:

//myInfoTable.rows.push({header_Sum:abc}); - doesn't work;

myInfoTable.rows.push({Sum_0:abc}); - works;

 

Your code works perfectly - thx.

 

I am at the beginning of the road - so sometimes my questions may look strange, but not all is clear for me at the moment.

 

regards

 

Krzysztof

use istead:

myInfoTable.rows.push({(""+header_Sum):abc}); - doesn't work;

Gucio
14-Alexandrite
(To:CarlesColl)

Typing:

myInfoTable.rows.push({(""+header_Sum):abc}); 

I receive invalid property id :(

Krzysztof

Build the row object before:

var newRow = {};
newRow[""+header_Sum] = abc;
myInfoTable.rows.push(newRow);

 

Gucio
14-Alexandrite
(To:CarlesColl)

thx

Krzysztof
slangley
23-Emerald II
(To:Gucio)

Hi @Gucio.

 

If the provided responses answered your question, please mark as Accepted Solution, for the benefit of others who may have the same question.

Regards.

 

--Sharon

Top Tags