Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I formated this data using c#, streamreader and writer and created this csv file. Here is the sample data:

A------B-C-----D------------E------------F-------G-------H---------I
NEW C A123 08/24/2011 08/24/2011 100.00 100.00 X123456 276135
NEW C A125 08/24/2011 08/24/2011 200.00 100.00 X123456 276135
NEW C A127 08/24/2011 08/24/2011 50.00 100.00 X123456 276135
NEW T A122 08/24/2011 08/24/2011 5.00 100.00 X225511 276136
NEW T A124 08/24/2011 08/24/2011 10.00 100.00 X225511 276136
NEW T A133 08/24/2011 08/24/2011 500.00 100.00 X444556 276137

I would like the following output:

NEW C A123 08/24/2011 08/24/2011 100.00 100.00 X123456 276135
NEW C A125 08/24/2011 08/24/2011 200.00 100.00 X123456 276135
NEW C A127 08/24/2011 08/24/2011 50.00 100.00 X123456 276135
NEW C A001 08/24/2011 08/24/2011 350.00 100.00 X123456 276135
NEW T A122 08/24/2011 08/24/2011 5.00 100.00 X225511 276136
NEW T A124 08/24/2011 08/24/2011 10.00 100.00 X225511 276136
NEW T A001 08/24/2011 08/24/2011 15.00 100.00 X225511 276136
NEW T A133 08/24/2011 08/24/2011 500.00 100.00 X444556 276137
NEW T A001 08/24/2011 08/24/2011 500.00 100.00 X225511 276137

With each change in field "I", I would like to add a line, sum column F, add a "A001" to C, and copy the contents
of the other fields into that newly ADDed line. The letters on the columns are for illustrative purposes only. There are no headers.

First, what should I do first? How do I sum column F, copy contents of all fields, and add "A001" to C? How do I add a line and copy the fields w/ each change in I?

Thank you in advance for your help.
Posted
Updated 1-Sep-11 14:12pm
v4
Comments
DPBNYC 1-Sep-11 21:03pm    
I know I left the commas out. Visualize them please.

1 solution

You commas are visualized :-). I feel pretty generous today. Do the following:


  1. Create a class or structure representing one line (record); let's call it Record.
  2. Use the list of those records. Literally, add:
    C#
    using RecordList = System.Collections.Generic.List<Record>;
  3. For reading a file, use System.IO.StreamReader; create it under "using" statement:
    C#
    RecordList list = new RecordList();
    using (System.IO.StreamReader reader = new System.IO.StreamReader(fileName) {
        //read lines here:
        string line = reader.ReadLine();
        //parse and validate each line
        Record record = //create instance of Record from parsed line
        list.Add(record);
    } //at this point, reader.Dispose will be called automatically, even if an exception was thrown
  4. Write a method of reading one line from a record, parsing and validating it. (It's shown as a comment above.) Start parsing using string.Split:
    C#
    //...
    string[] values = line.Split(',', System.StringSplitOptions.RemoveEmptyEntries);
    //convert strings into field of the type Record
    //use DateTime.Parse, double.Parse, uint.Parse, etc. See appropriate MSDN help pages
    //validate values as you parse
    //for example:
    record.F = double.Parse(values[5]); //may throw exception; decide how to handle it
  5. For finding sums, iterate through the list, for example:
    C#
    double sumF = 0d;
    /...
    foreach (Record record in list) {
        sum.F += record.F;
        //...
    }
  6. In case you might need an array for some other purposes, the previous step may look alternatively:
    C#
    Record[] records = list.ToArray();
    double sumF = 0d;
    /...
    foreach (Record record in records) {
        sum.F += record.F;
        //...
    }
  7. PROFIT!


—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900