Hesham Hassanein

Hesham Hassanein

  • NA
  • 140
  • 17k

splitting a Line from a file and saving in multidim. array

Sep 12 2016 7:47 AM

I would like to split the line from the csv file and save it into a multidimensional array but i am getting an error cannot convert type string into string[]. because i cannot say fields[][] = line.Split(|)

For example the line is:

1|2|3|4|5|6

1|6.9|GREATER THAN|%|29.844000|x|1

1|-27|LESS THAN|dBc|-30.51400|x|1

and i would like to subtract the third column from the second. Depending on the row number which is i in the following code. So i am using tryToparse as follows but the thing is that i want to access fields[i,2] and fields[i,1] and subtract or add them. but i cannot because i cannot use it in line.Split. How can i resolve this?. Thanks in advance

  1. while ((line = reader.ReadLine()) != null)  
  2.             {  
  3.               //  bJump = false;  
  4.                // Console.WriteLine(line);  
  5.                 string[] fields = line.Split('|');  
  6. // I want to say fields[][] = line.Split because i am //accessing fields[i,1] and [i,2] down in the code but i am //getting the error of cannot convert string to string[]  
  7.                 int parsed_field1;  
  8.                 int parsed_field2;  
  9.                   
  10.   
  11. for (int i = 0; i < count-1; i++)  
  12.                 {  
  13.   
  14.                     bool wasParsedOK1 = Int32.TryParse(fields[i,1], out parsed_field1);  
  15.                     bool wasParsedOK2 = Int32.TryParse(fields[i,2], out parsed_field2);  
  16.                      
  17.   
  18.                     Result[i, 0] = "PATS_TEST";  
  19.                     if (isheader)  
  20.                     {  
  21.                           
  22.                         isheader = false;                       //Data  
  23.                         Result[i, 1] = "Track ID";  
  24.                         Result[i, 2] = "DATUM";             //Datum from the header  
  25.                         Result[i, 3] = fields[i,1];             //Test Code  
  26.                         Result[i, 4] = "1";                     // Test Channel  
  27.                         Result[i, 5] = "Test Value";                
  28.                         Result[i, 6] = "1";                     //Test Frequency  
  29.                         Result[i, 7] = fields[i,5];             // Status  
  30.                         Result[i, 8] = "1";                     // Test Voltage  
  31.   
  32.   
  33.                         if (wasParsedOK1 && wasParsedOK2)  
  34.                         {  
  35.                             Result[i, 9] = (parsed_field1 - parsed_field2).ToString();                     // Lower Limit  
  36.                             Result[i, 10] = (parsed_field1 + parsed_field2).ToString();                   // Upper Limit  
  37.                              
  38.                         }  
  39.                         else if (!wasParsedOK2)  
  40.                         {   
  41.                         if (fields[2].Contains("GREATER THAN"))  
  42.                         {  
  43.                             Result[i, 9] = fields[i,1];  
  44.                             Result[i, 10] = (Int32.Parse(fields[1])+10).ToString();  
  45.   
  46.                         }  
  47.                         else if (fields[i,2].Contains("LESS THAN"))  
  48.                         {  
  49.                             Result[i, 9] = fields[i,1];  
  50.                             Result[i, 10] = "0";  
  51.   
  52.                         }  

Answers (5)