-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv.cs
More file actions
175 lines (166 loc) · 5.32 KB
/
csv.cs
File metadata and controls
175 lines (166 loc) · 5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace csvDifferences
{
class csv
{
public List<List<string>> rows;
public List<string> headers;
public int totalLines;
public Dictionary<string, int> countOfEachCompareFeild;
public List<string> groupColumn;
public csv(string path)
{
this.headers = new List<string>();
this.rows = new List<List<string>>();
int lineNum = 0;
this.countOfEachCompareFeild = new Dictionary<string, int>();
this.groupColumn = new List<string>();
using (StreamReader read = new StreamReader(path))
{
while (!read.EndOfStream)
{
string[] row = read.ReadLine().Split(',');
if (lineNum++ == 0)
{
foreach (string cell in row)
{
this.headers.Add(cell);
}
}
else
{
List<string> tosave = new List<string>();
foreach (string cell in row)
{
tosave.Add(cell);
}
this.rows.Add(tosave);
}
}
}
this.totalLines = lineNum;
}
public void populateCounts(string headerToCompare)
{
int index = this.headers.IndexOf(headerToCompare);
if (index != -1)
{
for (int i = 0; i < this.rows.Count; i++)
{
List<string> row = this.rows.ElementAt<List<string>>(i);
string col = row.ElementAt<string>(index);
if (this.countOfEachCompareFeild.ContainsKey(col))
{
this.countOfEachCompareFeild[col]++;
}
else
{
this.countOfEachCompareFeild.Add(col, 1);
}
}
}
}
public string[] getSortedListOfGroupings(string groupBy)
{
int index = this.headers.IndexOf(groupBy);
if (index != -1)
{
foreach (List<string> row in this.rows)
{
string col = row.ElementAt<string>(index);
this.groupColumn.Add(col);
}
}
this.groupColumn.Sort();
return this.groupColumn.ToArray<string>();
}
public string getRowForWrite(int index)
{
List<string> row = this.rows.ElementAt<List<string>>(index);
string result = "";
string delimiter = "";
row.ForEach(delegate(string cell)
{
result += delimiter + cell;
if (delimiter == "")
{
delimiter = ",";
}
});
return result;
}
public string getHeaderForWrite()
{
string result = "";
string delimiter = "";
headers.ForEach(delegate(string cell)
{
result += delimiter + cell;
if (delimiter == "")
{
delimiter = ",";
}
});
return result;
}
public string getRowForWrite(int index, string[] headers)
{
List<string> row = this.rows.ElementAt<List<string>>(index);
string result = "";
string delimiter = "";
foreach (string column in headers)
{
result += delimiter + row.ElementAt<string>(this.headers.IndexOf(column));
if (delimiter == "")
{
delimiter = ",";
}
}
return result;
}
public string getHeaderForWrite(string[] head)
{
string result = "";
string delimiter = "";
foreach (string column in head)
{
int index = this.headers.IndexOf(column);
if (index != -1)
{
result += delimiter + this.headers.ElementAt<string>(index);
if (delimiter == "")
{
delimiter = ",";
}
}
}
return result;
}
public int[] getRowIndexForValue(string header, string value)
{
List<int> results = new List<int>();
int index = this.headers.IndexOf(header);
if (index != -1)
{
for(int i=0;i<this.rows.Count;i++)
{
List<string> row = this.rows.ElementAt<List<string>>(i);
string col = row.ElementAt<string>(index);
if (col == value)
{
results.Add(i);
}
}
return results.ToArray<int>();
}
else
{
return new int[0];
}
}
}
}