-1

Hi Just a question...

foreach (string file in files)
{
    MessageBox.Show(String.Format("Name: {0}", file));
    List<string> newColumnData = new List<string>() { file };
    List<string> lines = File.ReadAllLines(file).ToList();
    lines[1] += ",Name";

    int index = 2;
    //add new column value for each row.
    lines.Skip(2).ToList().ForEach(line =>
    {
        //-1 for header
        MessageBox.Show(index.ToString());
        lines[index] += "," + newColumnData[index - 1];
        index++;
    });
    //write the new content
    File.WriteAllLines(file, lines);
}

The data looks like:(Header has 2 rows.)

7/1/2014 to 7/11/2014               
 Category            Trans Date  Post Date   Merchant Name           Amount
Dining Out            6/30/2014  7/1/2014    CLASSICS MARKET CAFE     8.41
Dining Out            7/1/2014   7/2/2014    CHIPOTLE                 12.07
Dining Out            7/3/2014   7/6/2014    THAI CUISI               18.24
Groceries             7/2/2014   7/3/2014    FESTIVAL FOODS           12.79

I want to add a new column name example "fileName", with the value "fileName"... just for the sake of convenience.

  1. I am receiving the same error at: lines[index] += "," + newColumnData[index - 1]; But the value of index is never <0 when checked through debugger/MessageBox.

  2. How to add the new column in the first position instead of last column?

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
khosla
  • 29
  • 1
  • 6

1 Answers1

0

So if your data has four lines, that means you'll be getting four iterations in the loop, which means it's trying to get values from array elements 1, 2, 3 and 4 of the array newColumnData. However, newColumnData only has one element, at index 0, right?

You should change the line in the middle of the ForEach loop to this, if you want the same value added for each row:

lines[index] += "," + newColumnData[0];

Finally, to add it at the beginning of the row, do this:

lines[index] = newColumnData[0] + "," + lines[index];
cf_en
  • 1,661
  • 1
  • 10
  • 18
  • Thank you very much all! I figured out the way by adding one more index for row. int index = 2, row = 2; //add new column value for each row. lines.Skip(2).ToList().ForEach(line => { //-2 for header lines[row] = newColumnData[index - 2] + "," + lines[row]; row++; }); – khosla Jul 13 '14 at 19:32
  • LOL. So index is initialized to 2, and never incremented, and you take 2 from that to access the right element of the newColumnData array? This is the same as the solution I gave you, except that you've added a new variable into the mix, which only serves to obscure what the code is doing and make it harder to follow. Drop the row variable and admit that newColumnData only has a single element. Better still, make newColumnData a string rather than a single element list of strings, and forget having to use an index to access it. – cf_en Jul 13 '14 at 19:41
  • I agree... I should have looked at your solution more intelligently or should I say I played dumb. And you're correct, I should take this into consideration and just drop the index. Thanks again. – khosla Jul 13 '14 at 20:03
  • EDIT: Just drop the index from my code, or follow your code. – khosla Jul 13 '14 at 20:05