Problem with arrays and list

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2008
Posts: 13
Reputation: jitupatil_2007 is an unknown quantity at this point 
Solved Threads: 0
jitupatil_2007 jitupatil_2007 is offline Offline
Newbie Poster

Problem with arrays and list

 
0
  #1
Apr 4th, 2008
hey friends i am reading a csv file and i have to read a specifice coloumn form the file and that coloumn contains date and i have to read the date first and then keep a validation that if the dates in the file are between some dates then the program will read data from another file . The csv file contains a header and that creates a problem while reading the first row the file the file data is like this:

start_date_time,ani,dialed_digits,actual_dur,rounded_dur_secs,cost
"03/01/2008 00:05:57",629172162448,"923455755684",2,2,0.002800
"03/01/2008 00:15:56",79279906564,"79278454880",51,60,0.135000
"03/01/2008 00:16:51",4166143724,"92922202502",188,188,0.256900
"03/01/2008 00:23:13",07956563557,"925871021085",1020,1020,1.393400
"03/01/2008 00:38:13",639262060046,"923084230440",1,1,0.001400
"03/01/2008 00:47:02",--------------,"92945623075",124,124,0.169400
"03/01/2008 01:08:14",7187696383,"92946725834",457,457,0.624300

The code of mine goes here
  1. static void Main(string[] args)
  2. {
  3. StreamReader sr = new StreamReader(@"C:\Documents and Settings\jitendra\Desktop\ric_mar.csv");
  4. StreamWriter sw = new StreamWriter(@"C:\dates.txt");
  5.  
  6. string dates = "";
  7. string[] dvalue = null;
  8. IList<string> list = new List<string>();
  9. while (!sr.EndOfStream)
  10. {
  11. dates = sr.ReadLine();
  12. dvalue = dates.Split(',',';');
  13. if (dvalue.Length > 3)
  14. {
  15. list.Add(dvalue[0].Replace("\"",""));
  16. }
  17. }
  18. DateTime begin = new DateTime(2008, 3, 1);
  19. DateTime end = new DateTime(2008, 3, 9);
  20.  
  21.  
  22. foreach (string var in list)
  23. {
  24. DateTime datevalue = DateTime.ParseExact(dvalue[0].ToString(), "3/1/2008 00:00:00", null);
  25. //DateTime datevalue = DateTime.Parse(var);
  26. Console.WriteLine(datevalue.ToString());
  27. Console.ReadLine();
  28. sw.WriteLine(var.ToString());
  29. }
  30. sw.Flush();
  31. sw.Close();
  32. }
  33.  
  34. }
  35. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: Problem with arrays and list

 
0
  #2
Apr 4th, 2008
If you managed to read the date column data so put them in array
2- remove the ' " ' from each string(date) using string.Remove("\""); method
3- convert the string to date and compare the two dates DateTime.Compare(date1, date2);
4- based on value returns from DateTime.Comapre -1, 0 or 1 go through execution some codes...
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 13
Reputation: jitupatil_2007 is an unknown quantity at this point 
Solved Threads: 0
jitupatil_2007 jitupatil_2007 is offline Offline
Newbie Poster

Re: Problem with arrays and list

 
0
  #3
Apr 5th, 2008
hi ramy i have written the code like this have a look at it:

  1.  
  2. StreamReader sr = new StreamReader(@"C:\Documents and Settings\jitendra\Desktop\Copy of ric_mar.csv");
  3. StreamWriter sw = new StreamWriter(@"C:\dates.txt");
  4.  
  5. string dates = "";
  6. string[] dvalue = null;
  7. IList<string> list = new List<string>();
  8. IList<DateTime> mydd = new List<DateTime>();
  9. while (!sr.EndOfStream)
  10. {
  11. dates = sr.ReadLine();
  12. dvalue = dates.Split(',',';');
  13. if (dvalue.Length > 3)
  14. {
  15. list.Add(dvalue[0].Replace("\"",""));
  16. }
  17. }
  18. foreach (string var in list)
  19. {
  20. DateTime datevalue = DateTime.ParseExact(var, "MM/dd/yyyy HH:mm:ss", null);
  21. sw.WriteLine(var.ToString());
  22. }
this prints the date in perfect manner now what i want is that if the array contains the date value between 03/01/2008 (mmddyyyy) to 03/09/2008 then the program should print the whole records which come in between these dates. so can you help me please..
Last edited by jitupatil_2007; Apr 5th, 2008 at 7:19 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 436
Reputation: JerryShaw is on a distinguished road 
Solved Threads: 72
JerryShaw JerryShaw is offline Offline
Posting Pro in Training

Re: Problem with arrays and list

 
0
  #4
Apr 6th, 2008
Option #1

  1. private void button2_Click(object sender, EventArgs e)
  2. {
  3. string sLine = "";
  4. DateTime dates;
  5. DateTime start = Convert.ToDateTime("03/01/2008 00:00:00");
  6. DateTime end = Convert.ToDateTime("03/09/2008 00:00:00");
  7. StreamReader sr = new StreamReader(@"C:\cvsParse.txt");
  8. StreamWriter sw = new StreamWriter(@"C:\dates.txt");
  9.  
  10. sw.WriteLine( sr.ReadLine() );
  11. while (!sr.EndOfStream)
  12. {
  13. sLine = sr.ReadLine();
  14. dates = Convert.ToDateTime(sLine.Replace("\"",string.Empty).Split(',')[0]);
  15. if (dates >= start && dates < end)
  16. sw.WriteLine(sLine);
  17. }
  18. sr.Close();
  19. sw.Flush();
  20. sw.Close();
  21.  
  22. }

Option #2
  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. string header = "";
  4. string dates = "";
  5. string[] dvalue = null;
  6.  
  7. StreamReader sr = new StreamReader(@"C:\cvsParse.txt");
  8. StreamWriter sw = new StreamWriter(@"C:\dates.txt");
  9.  
  10. DataTable dt = new DataTable("cvs");
  11. dt.Columns.Add("start_date_time", typeof(DateTime));
  12. dt.Columns.Add("origin");
  13. header = sr.ReadLine();
  14. while (!sr.EndOfStream)
  15. {
  16. dates = sr.ReadLine();
  17. dvalue = dates.Split(',');
  18. dt.Rows.Add(Convert.ToDateTime(dvalue[0].Replace("\"", string.Empty)) , dates );
  19. }
  20. sr.Close();
  21.  
  22. DataRow[] dr = dt.Select("start_date_time > '03/01/2008 00:00:01' and start_date_time < '03/01/2008 00:30:59'");
  23. sw.WriteLine(header);
  24. foreach (DataRow row in dr)
  25. sw.WriteLine(row["origin"].ToString());
  26.  
  27. sw.Flush();
  28. sw.Close();
  29.  
  30. }

Both examples create a new file with only the desired dates.

Option #1 simply copys the file into a new file where the date is in the desired range .
Option #2 is a little fancier, where it uses a DataTable to get the data, and allows you to write it out at one time. The datatable approach opens the door to all kinds of filtering, sorting, write out as XML, whatever you can do with a data table.

// Jerry
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC