0

I have a file with alot of numbers, each index has 4 subnumbers

no1 no2 no3 no4 
no1 no2 no3 no4 
no1 no2 no3 no4 

The file is a cvs file, but I need to read the numbers into an array as type double and make an interpolating, so I need to walkthrough the table.

Until now I have this, but I am stuck and do not really know how I nicely can convert the file to double values, so I can start calculate on them. Any suggestion ?

var filelines = from line in file.Skip(5)
                select line.Split(';');
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
Jedhi
  • 73
  • 1
  • 9

3 Answers3

1

You can split the line,parse each part into decimal and use SelectMany to flatten results:

file.Skip(5).SelectMany(line => line.Split(';').Select(decimal.Parse)).ToArray()
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
  • How can I make a Quattro array ? can i write double [][][][] = file.skip(5)... or any suggestion ? – Jedhi Feb 10 '15 at 11:59
1

If you want a double[][], so one array per line:

double d;
double[][] lineValues = file.Skip(5)
    .Select(l => l.Split(new[]{';'}, StringSplitOptions.RemoveEmptyEntries))
    .Where(arr => arr.Length == 4 && arr.All(s => double.TryParse(s, out d)))
    .Select(arr => arr.Select(double.Parse).ToArray())
    .ToArray();
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Thanks, linevalues is empty. I have tried to instantiate lineValues with lineValues = new lineValues(file.count), but does not Work. Do I miss something here ? And can I make it [][][][] so it is a Quattro array ? – Jedhi Feb 10 '15 at 11:57
  • @Jedhi: what is a quattro array? Why do you need such thing? In above `double[][]` every line is transposed into a `double[]` so that you get one array for every line contains all fields' values. Why it doesn't seem to work? Maybe because your file has a different structure. I have added a strong validation rule: `.Where(arr => arr.Length == 4 && arr.All(s => double.TryParse(s, out d)))`. Maybe the lines contain more than 4 fields or less or not all can be parsed to double. – Tim Schmelter Feb 10 '15 at 12:12
  • Hi Tim. it Works when I delete the line .Where(arr => arr.Length == 4 && arr.All(s => double.TryParse(s, out d))). The file looks like # # # # # 10; 2,0; 0; 0 30; 4; 0; 0 50; 6; 0; 0 70; 8; 0; 0 Structure is always the same. Will it try to parse to double if there are 4 numbers ? – Jedhi Feb 10 '15 at 14:34
  • @Jedhi: it is impossible to see the file structure in a comment. Please edit your question with sample lines in proper format(with line breaks and so on). It will only try to parse a line to a `double[]` if there are 4 numbers. – Tim Schmelter Feb 10 '15 at 14:39
  • It Works now. Thank you very much for your help and time. I appreciate that very much. Would have given you more points if possible ;-) – Jedhi Feb 10 '15 at 14:43
  • my fault. Should have accepted your answer now. Thanks Again. – Jedhi Feb 10 '15 at 14:47
  • If you want to have a multidimensional array [,] instead of jagged array [][] in the example above, how do you then write the linq statement ? – Jedhi Feb 18 '15 at 21:00
  • 1
    @Jedhi: multidimensional arrays and LINQ don't play well together. You can have a look here: http://stackoverflow.com/questions/18896150/c-sharp-linq-return-a-multidimensional-array-from-linq – Tim Schmelter Feb 18 '15 at 21:27
  • Thanks, I had a clue. – Jedhi Feb 18 '15 at 21:35
0

If there is certain values in each row, like let's say your cvs data store has a specific number of fields, s wiser and strongly typed move is to first make up a model for your data store, which according to the information you provided would be:

public class MyCVSModel {
    public Double Number1 {get; set;} 
    public Double Number2 {get; set;} 
    public Double Number3 {get; set;} 
    public Double Number4 {get; set;} 
}

Now, you can:

public static IEnumerable<MyCVSModel> Converion(String FileName){
     var AllLines = System.IO.ReadAllLines(FileName);
     foreach(var i in AllLines){
        var Temp = i.Split('\t'); // Or any other delimiter
        if (Temp.Lenght >= 4){ // 4 is because you have 4 values in a row
           List<Double> TryConversion = new List<Double>();
           foreach(var x in Temp) {
              if (IsDouble(x))
                 TryConversion.Add(Convert.ToDouble(x));
              else
                 TryConversion.Add(0);
           }
           MyCVSModel Model = new MyCVSModel();
           Model.Number1 = TryConversion[0];
           Model.Number2 = TryConversion[1];
           Model.Number3 = TryConversion[2];
           Model.Number4 = TryConversion[3];
           yield return Model;
        }
     }
}

public static Boolean IsDouble(String Value) {
    Regex R = new Regex(@"^[0-9]*(?:\.[0-9]*)?$");
    return R.IsMatch(Value);
}
Transcendent
  • 5,598
  • 4
  • 24
  • 47