Detecting repetition of part of received data - c#

Based on the code shown.. Am I writing the right coding if i want to compare the data that were being stream in? Basically starting from the part
while(serialPort1.IsOpen)
For instance first string of data received was T 12 29.5 then next string was T 12 29.5 followed by T 20 24.5 and on so.. basically unpredictable what going to be received next.
I want to program to be able to detect/count the number of appearance for the middle value..like...
====================
[number] | [Repeated times]
12 | 2
=================== but when another different number received,
[number] | [Repeated]
20 | 1
=================== the counter for the number will be overwrite and reset whenever a different number was received.
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
string time = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss.ff");
RxString = serialPort1.ReadLine();
string[] split = RxString.Split('.');
string dp = split[1];
Char c = dp[0];
split[1] = c.ToString();
RxString = split[0] + "." + split[1];
while (serialPort1.IsOpen)
{
string[] number = RxString.Split(' ');
string unit = number[1];
int count = 1;
for(int i = 1; i < unit.Count(); i++)
{
if(unit[i-1] == unit[i])
count++;
else
count = 1;
if(count == 4)
{
//execute some parameters
}
}
}
this.Invoke(new EventHandler(DisplayText));
StreamWriter MyStreamWriter = new StreamWriter(#"C:\Users\acer\Documents\Data3.txt", true);
MyStreamWriter.Write(time + " " + RxString + "\r\n");
MyStreamWriter.Flush();
MyStreamWriter.Close();
}
EDIT V2
Why wont the prog record data which only has count of 1?
string[] number = RxString.Split(' '); //split RxString by ' '
string unit = number[1]; //unit = unit no.
int count = 1;
for (int i = 1; i < unit.Count(); i++)
{
if (unit[i - 1] == unit[i])
count++;
else
{
count = 1;
StreamWriter MyStreamWriter = new StreamWriter(#"C:\Users\acer\Documents\Data3.txt", true); //True tell SW to append to file instead of overwriting
MyStreamWriter.Write(time + " " + RxString + "\r\n"); //Write time + string
MyStreamWriter.Flush();
MyStreamWriter.Close();
}

You should use a dictionary to store each element and its own count :
var dict = new Dictionary<string, int?>();
while (serialPort1.IsOpen)
{
string[] number = RxString.Split(' ');
string unit = number[1];
if (dict.ContainsKey(unit))
{
if (dict[unit].HasValue)
{
dict[unit]++;
if (dict[unit] == 4)
{
// execute some parameters
dict[unit] = null;
}
}
}
else
{
dict.Add(unit, 1);
}
}

I'd create a special struct for that:
struct DataInfo
{
public string Number { get; set; }
public int Counter { get; set; }
... Other data you require to work with
}
And use either List<DataInfo> or Dictionary<string, DataInfo> to store values;

Related

how do i create an if statement that outputs two names of students who have the same number of minutes from csv file

I am trying to output two names of students who have the same number of mins from a CSV file
input file :
ID,Last Name,First Name,Phone Number,Minutes
1,Doe,John,905-555-5555,1020
2,Barnett,Courtney,905-666-6666,112
3,Morrison,Jim,905-777-7777,912
4,Doe,Jane,905-222-2222,1020
5,Mitchell,Joni,416-333-3333,112
(ignore spaces between each line)
here Courtney and Joni have the same number of minimum minutes...i need to output both their names
my code is:
// open input file
StreamReader sr = new StreamReader("input.txt");
{
// initialize variables
int x=0,total= 0; double average=0;
string? titles,lastFirst;
string [ ] info, titlesSplit; int [ ] mins= new int [5]; string[,] myName = new string[5,5];
{
titles =sr.ReadLine();
titlesSplit = titles.Split(',');
for (x=0;x < 5; x++)
{
System.Console.Write(titlesSplit[x] + " ");
}
System.Console.WriteLine();
System.Console.WriteLine("-------------------------------------------------");
for(x = 0; x < 5 ; x++)
{
lastFirst = sr.ReadLine();
info = lastFirst.Split(',');
for (int k =0; k<5; k ++)
{
myName[x,k]= info[k];
}
}
int maxMin = Int32.MinValue;
int minMin = Int32.MaxValue;
string? maxName = " ";
string? maxLastName = " ";
string? minName=" ";
string? minLastName = " ";
for (int l = 0; l<5; l++)
{
for (int k =0; k <5;k++)
{
System.Console.Write(myName[l,k] + " ");
}
System.Console.WriteLine();
if (Convert.ToInt32(myName[l,4]) > maxMin)
{
maxMin = Convert.ToInt32(myName[l,4]);
maxName = myName[l,1];
maxLastName= myName[l,2];
//if statement to output two names of students who have the same number of mins
}
if(Convert.ToInt32(myName[l,4]) > minMin );
{
minMin= Convert.ToInt32(myName[l,4]);
minName= myName[l,1];
minLastName= myName[l,2];
//if statement to output two names of students who have the same number of
mins
}
total = (total + Convert.ToInt32(myName[l, 4]));
}
average = (total / 5);
System.Console.WriteLine("-------------------------------------------------");
System.Console.WriteLine("Minimum: " + minName +" " + minLastName +" " + minMin);
System.Console.WriteLine("Maximum: " + maxName +" " + maxLastName+ " "+ maxMin);
System.Console.WriteLine("Average: " + average);
}
}
sr.Close();
I took the liberty to change up your code a bit. For example, I think it would be a good idea to create a model class for your line entries:
public class Person
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string PhoneNumber { get; set; }
public int Minutes { get; set; }
}
The code to output the names of the entries, that have the smallest amount of minutes might then look something like this:
// open input file
using StreamReader sr = new StreamReader(Path);
List<Person> Persons = new();
_ = sr.ReadLine(); // ignore first line
while (!sr.EndOfStream)
{
var line = sr.ReadLine();
var values = line!.Split(',');
Persons.Add(new Person
{
ID = int.Parse(values[0]),
LastName = values[1],
FirstName = values[2],
PhoneNumber = values[3],
Minutes = int.Parse(values[4])
});
}
foreach(var person in Persons.OrderBy(p => p.Minutes).GroupBy(m => m.Minutes).First())
{
Console.WriteLine(person.FirstName);
}
It might be a good idea to add some more checks for the parsing and if you have big amounts of data, one might look into some performance improvements, but in general this should give some idea.

Reading text file until a space and storing several values

I am creating an application to read and display Pokemon stats. Currently, I have 6 txt files, one for each stat. I have 6 arrays reading each txt file and displaying each stat in a label.
I want to condense this information into a single text file, splitting each stat by a " " to keep each Pokemon's data on a single line.
Current code if it helps explain the idea better:
using System;
using System.Windows.Forms;
using System.IO;
namespace Pokedex
{
public partial class Pokedex : Form
{
public Pokedex()
{
InitializeComponent();
}
//Read stat data from text files into string arrays
string[] HP = File.ReadAllLines("HP.txt");
string[] Atk = File.ReadAllLines("Atk.txt");
string[] Def = File.ReadAllLines("Def.txt");
string[] SpAtk = File.ReadAllLines("SpAtk.txt");
string[] SpDef = File.ReadAllLines("SpDef.txt");
string[] Spe = File.ReadAllLines("Spe.txt");
private void cbxPokemon_SelectedIndexChanged(object sender, EventArgs e)
{
//Get array index of currently selected Pokemon
int index = cbxPokemon.SelectedIndex;
//Get integer values out of the string arrays for appropriate use
int intHP = int.Parse(HP[index]);
int intAtk = int.Parse(Atk[index]);
int intDef = int.Parse(Def[index]);
int intSpAtk = int.Parse(SpAtk[index]);
int intSpDef = int.Parse(SpDef[index]);
int intSpe = int.Parse(Spe[index]);
//Update labels with stat values in string forme, could also assign intStat.ToString()
lblDexNum.Text = (index + 1).ToString("d3");
lblHP.Text = HP[index];
lblAtk.Text = Atk[index];
lblDef.Text = Def[index];
lblSpAtk.Text = SpAtk[index];
lblSpDef.Text = SpDef[index];
lblSpe.Text = Spe[index];
lblBST.Text = (intHP + intAtk + intDef + intSpAtk + intSpDef + intSpe).ToString();
//Update bar width based on stat value
barHP.Width = intHP;
barAtk.Width = intAtk;
barDef.Width = intDef;
barSpAtk.Width = intSpAtk;
barSpDef.Width = intSpDef;
barSpe.Width = intSpe;
//Disable Previous and Next buttons when they cannot be used
if (index == 0) { btnPrev.Enabled = false; }
else { btnPrev.Enabled = true; }
if (index == cbxPokemon.Items.Count - 1) { btnNext.Enabled = false; }
else { btnNext.Enabled = true; }
}
private void btnPrev_Click(object sender, EventArgs e)
{
cbxPokemon.SelectedIndex -= 1;
}
private void btnNext_Click(object sender, EventArgs e)
{
cbxPokemon.SelectedIndex += 1;
}
}
}
Ideally, the file would contain the 6 stats, for example "100 90 80 70 60 50" on each line, with each of those valued being stored.
Is there a simple way to read until a space, store that value, and keep doing that until the end of the line?
Yes, this is very simple to do. You can use the String.Split() method to break apart the lines into parsable pieces.
You can read the stats file in the same way you are now, except you would only need to read one file instead of six:
string[] lines = File.ReadAllLines("PokemonStats.txt");
Then, inside your cbxPokemon_SelectedIndexChanged method, you can retrieve the stats for a Pokemon like this:
//Get integer values out of the string for appropriate use
string line = lines[index];
string[] parts = line.Split(' ');
int intHP = int.Parse(parts[0]);
int intAtk = int.Parse(parts[1]);
int intDef = int.Parse(parts[2]);
int intSpAtk = int.Parse(parts[3]);
int intSpDef = int.Parse(parts[4]);
int intSpe = int.Parse(parts[5]);
Of course this assumes there will always be exactly 6 integer stats per line and they will all be in a specific order.
Try something like this:
static void Main(string[] args)
{
//list to store stats
List<string[]> pokemonStats = new List<string[]>();
//get a reader on the file
using (StreamReader reader = new StreamReader("TextFile1.txt"))
{
//while we still have lines to read
while (!reader.EndOfStream)
{
//get the line of stats
string line = reader.ReadLine();
//split it on the ' ' character and store it in our list of pokemon stats
pokemonStats.Add(line.Split(' '));
}
}
//we have them all so do something, like print to screen
foreach (string[] pokemon in pokemonStats)
{
foreach (string stat in pokemon)
Console.Write(stat + " ");
Console.WriteLine();
}
Console.ReadLine();
}
Where TextFile1.txt contains; which also happens to be the output...
1 1 1 1 1 1
2 2 2 2 2 2
3 3 3 3 3 3
Ideally, the file would contain the 6 stats, for example "100 90 80 70 60 50" on each line, with each of those valued being stored.
So this solution is actually very simple, assuming you have a file that looks like this:
100
90
80
70
60
50
You could write code that looks like this:
public string[] ReadStats(string fileName)
{
return File.ReadAllLines(fileName);
}
This would return:
[ "100", "90", "80", "70", "60", "50" ]
This is obviously an over-simplified version that does not checking on whether the file exists, whether there are more than 6 lines, etc. But it gets the idea across, and you could enhance it to suit your needs.
The answer to your main question:
Is there a simple way to read until a space, store that value, and keep doing that until the end of the line?
This is a bit more complicated, but this should work:
public IEnumerable<string[]> ReadAllPokemonStats(string fileName)
{
List<string[]> allPokemon = new List<string[]>();
string[] allStats = new string[6];
var allText = File.ReadAllText(fileName);
int nextStatIndex = 0;
string thisStat;
for(int i=0; i < allText.Length; i++)
{
var nextChar = allText[i];
if(nextChar == ' ')
{
allStats[nextStatIndex] = thisStat;
nextStatIndex++;
continue;
}
if(nextChar == '\r')
{
allPokemon.Add(allStats);
nextStatIndex = 0;
allStats = new string[6];
continue;
}
thisStat += nextChar.ToString();
}
return allPokemon;
}
Again, the above code is not perfect, for example it reads the entire file into memory so there's a potential attack vector there. It also does not ensure that the stats are actually numeric, but neither did your code (it would just blow up on the int.Parse()). But again, it gives you the general idea. You can see this approach is actually a lot more complex that reading each stat line by line.
I am thinking that each file is a one column file that represents one type of stat. If this is correct, you can try this:
Perhaps creating a class the represents all the stats that you are loading and then treat that as an array or a dictionary to fetch the proper stat or to create a single stat file.
public class PokemonStat
{
public int Hp { get; set; }
public int Atk { get; set; }
public int Def { get; set; }
public int SpAtk { get; set; }
public int SpDef { get; set; }
public int Spe { get; set; }
}
Then in your executing file:
var newPokemanData = new Dictionary<int, PokemonStat>();
var fileNames = new string[] { "Hp.txt", "Atk.txt", "Def.txt", "SpAtk.txt", "SpDef.txt", "Spe.txt" }
foreach (var fileName in fileNames)
{
var lineNumber = 0;
using (var stream = new StreamReader(fileName))
{
while (!stream.EndOfStream)
{
var singleStat = stream.ReadLine();
if (!newPokemanData.Keys.Contains(lineNumber))
{
newPokemanData.Add(lineNumber, new PokemonStat());
}
switch(fileName)
{
case "Hp.txt":
newPokemanData[lineNumber].Hp = int.Parse(singleStat);
break;
case "Atk.txt":
newPokemanData[lineNumber].Atk = int.Parse(singleStat);
break;
case "Def.txt":
newPokemanData[lineNumber].Def = int.Parse(singleStat);
break;
case "SpAtk.txt":
newPokemanData[lineNumber].SpAtk = int.Parse(singleStat);
break;
case "SpDef.txt":
newPokemanData[lineNumber].SpDef = int.Parse(singleStat);
break;
case "Spe.txt":
newPokemanData[lineNumber].Spe = int.Parse(singleStat);
break;
default:
Console.WriteLine("Error");
break;
}
lineNumber++;
}
}
}
using (var unifiedStats = new StreamWriter("unifieldFile.txt"))
{
foreach (var line in newPokemanData.Keys)
{
//write to a file
unifiedStats.WriteLine(newPokemanData[line].Hp.ToString() + " " +
newPokemanData[line].Atk.ToString() + " " +
newPokemanData[line].Def.ToString() + " " +
newPokemanData[line].SpAtk.ToString() + " " +
newPokemanData[line].SpDef.ToString() + " " +
newPokemanData[line].Spe.ToString() + " "
);
}
}
//

Parse unique string from complicated serial data C#

I need to parse this string from serial:-
!00037,00055#
00037 as one string, 00055 as another string
However this string is came out when the robot's tire is rotated and some other string may also display before and after the string that I need to parse. For example this is the some of the transmission received:-
11,00085#R-STOPR-STOP!00011,00095#!00001,00015#R-STOP!00001,00085#!00003,00075#!00006,00015#R-STOP!00009,00025#!00011,00035#!00011,00085#R-STOPR-STOP!00011,00095#!00001,00015#R-STOP!00001,00085#!00003,00075#!00006,00015#R-STOP!00009,00025#!00011,00035#R-STOP!00001,00085#!00003,00075#!00006,00015#R-STOP!00009,00025#!00011,00035#R-STOP!00037,00055#!00023,00075#R-STOPR-STOP!00022,00065#!00011,00085#R-STOPR-STOP!00011,00095#!00001,00015#R-STOP!00001,00085#!00003,00075#!00006,00015#R-STOP!00009,00025#!00011,00035#R-STOP!00037,00055#!00023,00075#R-STOPR-STOP!00022,00065#!00011,00085#R-STOPR-STOP!00011,00095#!00001,00015#
So far I'm stuck at what to do next after SerialPort.ReadExisting()
Here is some code to retrieve the serial data:-
private void serialCom_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
InputData = serialCom.ReadExisting();
if (InputData != String.Empty)
{
this.BeginInvoke(new SetTextCallback(IncomingData), new object[] { InputData });
}
}
catch
{
MessageBox.Show("Error");
}
}
and display incoming serial data inside textbox
private void IncomingData(string data)
{
tb_incomingData.AppendText(data);
tb_incomingData.ScrollToCaret();
}
This code is using .NET Framework 4.0 and Windows Form.
Finally solve it using indexof and substring.
private void IncomingData(string data)
{
//Show received data in textbox
tb_incomingData.AppendText(data);
tb_incomingData.ScrollToCaret();
//Append data inside longdata (string)
longData = longData + data;
if (longData.Contains('#') && longData.Contains(',') && longData.Contains('!'))
{
try
{
indexSeru = longData.IndexOf('!'); //retrieve index number of the symbol !
indexComma = longData.IndexOf(','); //retrieve index number of the symbol ,
indexAlias = longData.IndexOf('#'); //retrieve index number of the symbol ,
rotation = longData.Substring(indexSeru + 1, 5); //first string is taken after symbol ! and 5 next char
subRotation = longData.Substring(indexComma + 1, 5); //second string is taken after symbol ! and 5 next char
//tss_distance.Text = rotation + "," + subRotation;
longData = null; //clear longdata string
}
catch
{
indexSeru = 0;
indexComma = 0;
indexAlias = 0;
}
}
}
You can determine your pattern to transform this string into a array using SPLIT function.
This code, send "!00037,00055#" returns two itens: 00037 and 00055.
static void Main(string[] args)
{
string k = "!00037,00055#";
var array = k.ToString().Split(',');
Console.WriteLine("Dirty Itens");
for (var x = 0; x <= array.Length - 1; x++)
{
var linha = "Item " + x.ToString() + " = " + array[x];
Console.WriteLine(linha);
}
Console.WriteLine("Cleaned Itens");
for (var x = 0; x <= array.Length - 1; x++)
{
var linha = "Item " + x.ToString() + " = " + CleanString(array[x]);
Console.WriteLine(linha);
}
Console.ReadLine();
}
public static string CleanString(string inputString)
{
string resultString = "";
Regex regexObj = new Regex(#"[^\d]");
resultString = regexObj.Replace(inputString, "");
return resultString;
}

Saving Specific Lines to New Text Files

I am busy working with text files that are structured as seen below:
This is rainfall data in a continuous string each 5 characters after the date represent a day in the month.
0005880 W 1926 9-7777-7777-7777-7777-7777-7777-7777-7777-7777 117 130 64-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777
0005880 W 192610-7777-7777-7777-7777-7777-7777-7777-7777-7777 23-7777-7777-7777-7777 3-7777 226 462 71-7777-7777 157 76 15-7777-7777-7777-7777-7777-7777-7777
0005880 W 192611 3 20-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777 61 142-7777-7777-7777 8-7777-7777-7777-7777
0005880 W 192612-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777 132-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777
The year and month are represented in (10, 4) and (14, 2) positions within the string.
My problem is that there is instances when the next line isn't the month that is meant to follow. I have written code that adds a line where a month of data is missing shown bellow.
public void findGapsToolStripMenuItem_Click(object sender, EventArgs e)
{
TabPage tp = new TabPage();
RichTextBox rtb = new RichTextBox();
rtb.Dock = DockStyle.Fill;
rtb.Multiline = true;
rtb.AcceptsTab = true;
rtb.WordWrap = false;
Stream myStream;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
tp.Controls.Add(rtb);
tabControl1.TabPages.Add(tp);
string strfilename = openFileDialog1.FileName;
string[] lines = File.ReadAllLines(strfilename);
string[] pathArr = strfilename.Split('\\');
string[] fileArr = pathArr.Last().Split();
string filen = fileArr.Last().ToString();
tp.Text = filen;
int pyear = 0;
int pmon = 0;
int imon = 0;
int iyear = 0;
foreach (string line in lines)
{
string missing = "-9999";
string year = line.Substring(10, 4);
string mon = line.Substring(14, 2);
iyear = Convert.ToInt32(year);
imon = Convert.ToInt32(mon);
if (pyear == 0)
{
pyear = iyear;
pmon = imon;
rtb.AppendText(line + "\n");
}
else
{
int pt = pyear * 12 + pmon;
int t = iyear * 12 + imon;
if ((pt + 1) == t)
{
rtb.AppendText(line + "\n");
}
else
{
rtb.AppendText("Missing Months =" + (t - pt) + "\n");
}
if (line.Contains(missing))
{
rtb.AppendText("Missing Days" + "\n");
}
pyear = iyear;
pmon = imon;
}
rtb.SelectAll();
rtb.SelectionAlignment = HorizontalAlignment.Left;
rtb.SelectionFont = new Font("Consolas", 10);
}
}
}
}
My Question is, Is there a way of exporting all the lines before the missing month or day to a text file named the start date to the date before the missing month or day. E.g 1926.9.1926.10.txt. Then continuing through the file for the next section of data before the next missing month or day. So essentially ending up with multiple text documents containing Years or Months of Data with out gaps. I would also like it to automatically create a folder with the Station Number which is the first 14 characters (i.E 0005880 W) where all the text files will be created in.
Update
public void findGapsToolStripMenuItem_Click(object sender, EventArgs e)
{
TabPage tp = new TabPage();
RichTextBox rtb = new RichTextBox();
rtb.Dock = DockStyle.Fill;
rtb.Multiline = true;
rtb.AcceptsTab = true;
rtb.WordWrap = false;
Stream myStream;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
tp.Controls.Add(rtb);
tabControl1.TabPages.Add(tp);
string strfilename = openFileDialog1.FileName;
string[] lines = File.ReadAllLines(strfilename);
string[] pathArr = strfilename.Split('\\');
string[] fileArr = pathArr.Last().Split();
string filen = fileArr.Last().ToString();
string pat = #"C:\Test\" + filen;
System.IO.Directory.CreateDirectory(pat);
int i;
tp.Text = filen;
int pyear = 0;
int pmon = 0;
int imon = 0;
int iyear = 0;
int j = 1;
foreach (string line in lines)
{
using (StreamWriter sw = new StreamWriter(#"C:\Test\" + filen+".txt"))
{
string missing = "-9999";
string year = line.Substring(10, 4);
string mon = line.Substring(14, 2);
iyear = Convert.ToInt32(year);
imon = Convert.ToInt32(mon);
string filepath = #"C:\Test\" + year + "." + mon+".txt";
if (pyear == 0)
{
File.CreateText(filepath);
pyear = iyear;
pmon = imon;
rtb.AppendText(line + "\n");
sw.WriteLine(line);
}
else
{
File.CreateText(filepath);
int pt = pyear * 12 + pmon;
int t = iyear * 12 + imon;
if ((pt + 1) == t)
{
rtb.AppendText(line + "\n");
sw.WriteLine(line);
}
else
{
string path = pat + "\\" + year + "." + mon + ".txt";
File.CreateText(path);
rtb.AppendText("Missing Months =" + (t - pt) + "\n");
}
if (line.Contains(missing))
{
string path = pat + "\\" + year + "." + mon + ".txt";
File.CreateText(path);
rtb.AppendText("Missing Days" + "\n");
}
pyear = iyear;
pmon = imon;
}
rtb.SelectAll();
rtb.SelectionAlignment = HorizontalAlignment.Left;
rtb.SelectionFont = new Font("Consolas", 10);
}
}
}
}
}
You can create files using the various methods of the System.IO.File class:
http://msdn.microsoft.com/en-us/library/system.io.file.aspx
This class includes methods for creating files as well as writing out arbitrary text lines to one.
You can create directories using the methods of the System.IO.Directory class:
http://msdn.microsoft.com/en-us/library/system.io.directory.aspx
UPDATE: Here is some pseudocode
startdate = null
foreach(line in the input file)
{
currentdate = date on this line in the input file
if(startdate == null)
{
// We are at the start of a new block of dates
startdate = currentdate
add this line to a list (in memory)
}
else if(currentdate == lastdate in the list + 1 month)
{
// This date is consecutive
add this line to a list (in memory)
}
else
{
// We have a gap in the data
write out all data in the list to file named <startdate>-<lastdate in list>
startdate = currentdate
add this line to the list (which we've just emptied)
}
}
write out the last file
This is just very rough and ready, but should indicate the kind of way you need to think to write this code. One thing to be clear on, if you want to name the file using the end date of a block of dates you can't create the file until you've found the last line in that block so you need to store the lines in memory until you find a gap in the dates, or the end of your input file.

How do i get the numbers from the GetKey back to the List<float>? (Reverse of what i did)

This is the code for the SetKey i loop over the Lists take out the numbers convert to string and put it in to the SetKey now i need to reverse the action using GetKey and put back the numbers to the Lists so eahc List Point_X and Point_Y will have the numbers as before.
string[] xFrames = new string[wocl.Count];
string[] yFrames = new string[wocl.Count];
string X="";
string Y="";
for (int i = 0; i < wocl.Count; i++)
{
X = string.Format("Frame_X_{0} ", i + 1);
Y = string.Format("Frame_Y_{0} ", i + 1);
for (int j = 0; j < wocl[i].Point_X.Count; j++)
{
xFrames[i] += string.Format("{0},", wocl[i].Point_X[j]);
yFrames[i] += string.Format("{0},", wocl[i].Point_Y[j]);
}
string tt = xFrames[i].Trim(",".ToCharArray());
string yy = yFrames[i].Trim(",".ToCharArray());
setting_file.SetKey(X, tt);
setting_file.SetKey(Y, yy);
}
Now tt is a string of number for example 122,33,44,55,121
Now i need to parse the numbers back. Now i need to take the string and parse the numbers and put them back to a float List:
List a = setting_file.GetKey(X);
But X is a key that present a string of numbers not a List of numbers.
This is the code in the OptionsFile of the functions GetKey and SetKey:
/*----------------------------------------------------------
* Function : GetKey
* Description : gets the value of the key.
* Parameters : key
* Return : value of the key if key exist, null if not exist
* --------------------------------------------------------*/
public string GetKey(string key)
{
// string value_of_each_key;
string key_of_each_line;
string line;
int index;
string key_value;
key_value = null;
sr = new StreamReader(Options_File);
while (null != (line = sr.ReadLine()))
{
index = line.IndexOf("=");
// value_of_each_key = line.Substring(index+1);
if (index >= 1)
{
key_of_each_line = line.Substring(0, index);
if (key_of_each_line == key)
{
key_value = line.Substring(key.Length + 1);
}
}
else
{
}
}
sr.Close();
return key_value;
}
/*----------------------------------------------------------
* Function : SetKey
* Description : sets a value to the specified key
* Parameters : key and a value
* Return : none
* --------------------------------------------------------*/
public void SetKey(string key , string value)
{
bool key_was_found_inside_the_loop;
string value_of_each_key;
string key_of_each_line ;
string line;
int index;
key_was_found_inside_the_loop = false;
temp_settings_file = "\\temp_settings_file.txt";
temp_settings_dir = path_exe + #"\temp_settings";
if (!Directory.Exists(temp_settings_dir))
{
Directory.CreateDirectory(temp_settings_dir);
}
sw = new StreamWriter(temp_settings_dir+temp_settings_file);
sr = new StreamReader(Options_File);
while (null != (line = sr.ReadLine()))
{
index = line.IndexOf("=");
key_of_each_line = line.Substring(0, index);
value_of_each_key = line.Substring( index + 1);
// key_value = line.Substring(0,value.Length);
if (key_of_each_line == key)
{
sw.WriteLine(key + " = " + value);
key_was_found_inside_the_loop = true;
}
else
{
sw.WriteLine(key_of_each_line+"="+value_of_each_key);
}
}
if (!key_was_found_inside_the_loop)
{
sw.WriteLine(key + "=" + value);
}
sr.Close();
sw.Close();
File.Delete(Options_File);
File.Move(temp_settings_dir + temp_settings_file, Options_File);
return;
}
What i need is that in the List a it will contain the numbers from the string X
X is like a key the result in SetKey function is a Key = Value
For example : Hello = 122,33,44,55,66 Hello is like the variable X its the key and on the right hand the numbers are the key values.
So now i need to get the key X values and put them into the List
Cant figure out how to do it.
If before i had a List and i loop over it and took out the numbers from the List and created a string of the numbers and put them in the SetKey now i need to use the GetKey and take the numbers and put them back to the List
Edit:
public void Load(string path,string fileName)
{
string X = "";
string t = path + "\\" + fileName;
OptionsFile setting_file = new OptionsFile(t);
for (int i = 0; i <= wocl.Count ; i++)
{
X = string.Format("Frame_X_{0} ", i + 1);
}
string test = setting_file.GetKey(X);
}
Thep roblem is that if im running in the loop on the List wocl so when im running the program this List is count 0 or 1. But in the GetKey in the text file i might have 4 frames or 1 frame i mean how do i know on how much to count for in the loop ?
I tried with the wocl List for the test but now in the string test im getting the numbers of the first Frame_X_1 but thats it.
While in hte file it self it looks like:
Frame_X_1 =332,325,336,334,332,325,333,328,332
Frame_Y_1 =218,217,202,212,211,210,204,202,204
Frame_X_2 =270,325,336,347,321,325,333,328,332
Frame_Y_2 =257,217,202,282,156,210,204,202,204
Frame_X_3 =270,325,336,347,321,336,270,371,332
Frame_Y_3 =257,217,202,282,156,250,199,135,204
I mean when im running the program all the Lists are empty count to 0 and yet i need to retrive each key Frame_X_1 then Frame_Y_1 and so on...And i dont know how many keys there are.
Add this to a static helper class
public static List<T> ToListOf<T>(this IEnumerable enumerable)
{
var list = new List<T>();
foreach (var item in enumerable)
{
list.Add(item.ConvertTo<T>());
}
return list;
}
then: var myFloats = tt.Split(',').ToListOf<float>();
elegant, no? :)
EDIT
I forgot one more extension method:
public static T ConvertTo<T>(this object source)
{
return (T)Convert.ChangeType(source, typeof(T));
}
Suppose you have a string like "123,33.44.55.66" and you know that this string is comma delimited. to retrieve the numbers:
string str = "123,33,44,55,66";
string[] strArray = str.Split(',');
you can then convert the strArray to any type compatible.

Categories