Reputation: 45
So I had some assistance with this yesterday but now I needed to make a change and my change doesn't seem to work. Wanted to see if someone would help me with what I'm doing wrong.
So here is an example of the file read in
Now originally this was reading the FSD number of 0.264
then reading the first number in the first column of 3.4572
and the last number of that column. This worked fine. But now I know longer need that FSD number and I no longer need the first number 3.4572
. Instead I need the first number that doesn't have 0.00
in the last column, and then the last number still. So this is what I have but it isn't grabbing anything at all, if (dataWithAvgVolts.Count() > 1)
is skipped.
public partial class FrmTravelTime : Form
{
const string FSD__Line_Identifier = "Drilling Data";
const string Data_Start_Point_Identifier = "AVG_VOLTS";
const string Spark_Point_Identifier = "0.00";
static readonly char[] splitter = { ' ', '\t' };
public FrmTravelTime()
{
InitializeComponent();
}
private void btnCalculate_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in DGV_Hidden.Rows)
{
FileInfo info = new FileInfo();
{
var lines = File.ReadAllLines(row.Cells["colfilelocation"].Value.ToString());
var fsdLine = lines.FirstOrDefault(line => line.Contains(FSD__Line_Identifier));
var dataWithAvgVolts = lines.SkipWhile(line => line.Contains(Data_Start_Point_Identifier + Spark_Point_Identifier + FSD__Line_Identifier)).ToList();
if (dataWithAvgVolts.Count() > 1)
{
var data = dataWithAvgVolts[1].Split(splitter);
info.startvalue = Convert.ToDouble(data[0]);
data = dataWithAvgVolts[dataWithAvgVolts.Count - 1].Split(splitter);
info.endvalue = Convert.ToDouble(data[0]);
}
info.finalnum = info.startvalue - info.endvalue;
}
}
}
public class FileInfo
{
public double startvalue;
public double endvalue;
public double firstnum;
public double finalnum;
}
So you see here the first line that finally does not have 0.00
is
3.0164 7793 1 0 0.159 0.02
So my info.startvalue
will be 3.0164
This goes down a good 100ish lines until the last line of
2.7182 8089 0 0 0.015 22.19
So my info.endvalue
should be 2.7182
@Amit
var lines = File.ReadAllLines(row.Cells["colfilelocation"].Value.ToString());
var fsdLine = lines.FirstOrDefault(line => line.Contains(FSD__Line_Identifier));
info.FSD = fsdLine.Substring(fsdLine.IndexOf(FSD_Identifier) + FSD_Identifier.Length, 7);
var dataWithAvgVolts = lines.SkipWhile(line => !line.Contains(Data_Start_Point_Identifier)).ToList();
var data = lines.Where(line => (!line.Contains(Data_Start_Point_Identifier) && !line.Contains(FSD__Line_Identifier) && !line.EndsWith("0.00"))).ToList();
if (data.Count > 1)
{
var line = data[0];
var tempdata = data[0].Split(splitter);
info.startvalue = Convert.ToDouble(tempdata[0]);
var thisdata = data[data.Count - 1].Split(splitter);
info.endvalue = Convert.ToDouble(thisdata[0]);
}
Upvotes: 3
Views: 237
Reputation: 4475
Your SkipWhile condition is incorrect.
line => !line.Contains(Data_Start_Point_Identifier + Spark_Point_Identifier + FSD__Line_Identifier)
Will mean AVG_VOLTS0.00Drilling Data
. You are saying, skip the text file till the line does not contain AVG_VOLTS0.00Drilling Data. You do not have this is one line anywhere. So, basically, you seem to be skipping the entire file. Put up a else condition, that will work.
This line returns all rows which do not have "Drilling Data" word, "AVG_VOLTS" word, and do not end with "0.00".
var data = lines.Where(line=>(!line.Contains(Data_Start_Point_Identifier) && !line.Contains(FSD__Line_Identifier) && !line.EndsWith("0.00"))).ToList();
Then, take the rows, and process.
If condition can be
if (data.Count > 1)
{
var line = data[0];
var tempdata = data[0].Split(splitter);
info.startvalue = Convert.ToDouble(tempdata[0]);
var thisdata = data[data.Count - 1].Split(splitter);
info.endvalue = Convert.ToDouble(thisdata[0]);
}
Upvotes: 1