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(';');
6
  • Can you show us an example of your input and the desired output? Commented Feb 10, 2015 at 11:26
  • What is the desired output ? Commented Feb 10, 2015 at 11:26
  • why are you using Skip(5) ? if you are trying to skip header shouldn't it be Skip(1) ? Commented Feb 10, 2015 at 11:28
  • Do you need to retain the "lines", so one double[] per line or one double[] overall? Commented Feb 10, 2015 at 11:29
  • 1
    @Selman22: sometimes the first lines contain not only the header but also file informations or summaries. Commented Feb 10, 2015 at 11:30

3 Answers 3

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();
Sign up to request clarification or add additional context in comments.

9 Comments

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: 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.
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: 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.
@Jedhi: multidimensional arrays and LINQ don't play well together. You can have a look here: stackoverflow.com/questions/18896150/…
|
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()

1 Comment

How can I make a Quattro array ? can i write double [][][][] = file.skip(5)... or any suggestion ?
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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.