You should use an existing csv-parser like this.
However, assuming that the name of the pattern is unique and you want to acess it later:
var gxDict = new Dictionary<string, List<Tuple<double, double>>>();
List<Tuple<double, double>> currentGxList = null;
foreach (string line in File.ReadLines(filePath))
{
if (line.StartsWith("#Pattern"))
{
string[] headers = line.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
string patternField = headers.First();
string patterName = string.Join(" ", patternField.Split().Skip(1).Take(2));
List<Tuple<double, double>> gxList = null;
if (gxDict.TryGetValue(patterName, out gxList))
currentGxList = gxList;
else
{
currentGxList = new List<Tuple<double, double>>();
gxDict.Add(patterName, currentGxList);
}
}
else
{
if (currentGxList != null)
{
string[] values = line.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
double gx1;
double gy1;
string gx1Str = values.ElementAtOrDefault(3);
string gy1Str = values.ElementAtOrDefault(4);
if (double.TryParse(gx1Str, out gx1) && double.TryParse(gx1Str, out gy1))
{
currentGxList.Add(Tuple.Create(gx1, gy1));
}
}
}
}
// now you can access it in this way:
List<Tuple<double, double>> nameItem1Vals = gxDict["Name Item1"];
foreach (var xy in nameItem1Vals)
Console.WriteLine("gx1: {0} gy1: {1}", xy.Item1, xy.Item2);
// or in a loop:
foreach (var kv in gxDict)
{
string pattern = kv.Key;
Console.WriteLine("pattern: {0}", pattern);
foreach (var xy in nameItem1Vals)
Console.WriteLine("gx1: {0} gy1: {1}", xy.Item1, xy.Item2);
}
Note that i avoid Linq when it comes to IO
where you need event handling on file level and when i try to parse input which could also cause exceptions.