0

I'm dealing with a slightly specific problem. I have a program which grabs tables from the web in JSON format and converts them to CSVs. The tables are dynamic in size and include a header row with text. One table might look like:

DataCode,AreaName,AreaID,Population
G1000,Billford,15,21000
G1001,Millville,18,21000

Assuming I don't know how big the array is going to be, I would like to be able to take the sum of ONLY the population statistic. (I will have the header for the column for which I want the data stored in a variable, in this case "population" but it changes dynamically as well.)

When consolidating multiple tables, I also have the issue that duplicate entries are difficult to remove. For example a combined table may look like:

DataCode,AreaName,AreaID,Population
G1000,Billford,15,21000
G1001,Millville,18,21000
DataCode,AreaName,AreaID,Population
G1003,Brinton,21,26000
G1004,Nixon,24,12000

I'm mystified by how to create a function that can take a table of any size in CSV or JSON format, and take the sum of only the numbers in a given column.

UpQuark
  • 791
  • 1
  • 11
  • 35
  • If you have MS Excel installed, which can read CSV, you would simply highlight the column you want, and your sum is displayed in the status bar :) – ardnew Jul 25 '12 at 15:59
  • 1
    Do you have a CSV reader? If so, then just retrieve the Population column and sum it until there are no more rows. If not, search this site for examples of it. Am I missing something? – Anon Mail Jul 25 '12 at 16:12
  • You can use one of the solutions to [this question](http://stackoverflow.com/questions/1120140/csv-parser-in-c), and then sum up the 4th column. Non numerical input will just be zero when you try to convert it with `atoi` or `strtol`. – jxh Jul 25 '12 at 16:14

0 Answers0