Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Aug 25th, 2008 | Syntax: C# | Size: 3.00 KB | Hits: 194 | Expires: Never
Copy text to clipboard
  1. using System;
  2. using System.Text.RegularExpressions;
  3. using System.Data;
  4. using System.Collections;
  5. namespace retest
  6. {
  7.     class Program
  8.     {
  9.  
  10.         static void Main(string[] args)
  11.         {
  12.             System.IO.StreamReader sr = new System.IO.StreamReader("sample.txt");
  13.             String fileContent = sr.ReadToEnd();
  14.             sr.Close();
  15.             String cp = @"\s+(-?[\d.]+)";
  16.             Regex re = new Regex(@"^(\S+)"+cp+cp+cp+cp+cp+cp+cp+cp+".*", RegexOptions.Multiline);
  17.             MatchCollection mc = re.Matches(fileContent);
  18.             DataTable table = new DataTable();
  19.             DataColumn CodeColumn = table.Columns.Add("Code", typeof(string));
  20.             table.Columns.Add("Open", typeof(float));
  21.             table.Columns.Add("High", typeof(float));
  22.             table.Columns.Add("Low", typeof(float));
  23.             table.Columns.Add("Close", typeof(float));
  24.             table.Columns.Add("Change", typeof(float));
  25.             table.Columns.Add("Trade", typeof(float));
  26.             table.Columns.Add("Volume", typeof(float));
  27.             table.Columns.Add("Value", typeof(float));
  28.             table.Columns.Add("Line", typeof(string));
  29.             foreach (Match m in mc)
  30.             {
  31.                 table.Rows.Add(new object[] { m.Groups[1].Value, m.Groups[2].Value, m.Groups[3].Value, m.Groups[4].Value, m.Groups[5].Value, m.Groups[6].Value, m.Groups[7].Value, m.Groups[8].Value, m.Groups[9].Value, m.Groups[0].Value});
  32.             }
  33.             table.AcceptChanges();
  34.             DataRow[] allRows;
  35.             allRows = table.Select();
  36.             DataRow[] foundRows;
  37.             ArrayList list = new ArrayList();
  38.             for (int ii = 0; ii < allRows.Length; ii++)
  39.             {
  40.                 if (!list.Contains(allRows[ii]["Code"]))
  41.                 {
  42.                     list.Add(allRows[ii]["Code"]);
  43.                     string expression = "Code = '" + allRows[ii]["Code"] + "'";
  44.                     foundRows = table.Select(expression);
  45.                     if (foundRows.Length == 2)
  46.                     {
  47.                         Console.WriteLine(expression + ":");
  48.                         Console.WriteLine("row 0:");
  49.                         Console.WriteLine(foundRows[0]["Code"] + "," + foundRows[0]["Open"] + "," + foundRows[0]["High"] + "," + foundRows[0]["Low"] + "," + foundRows[0]["Close"] + "," + foundRows[0]["Change"] + "," + foundRows[0]["Trade"] + "," + foundRows[0]["Volume"] + "," + foundRows[0]["Value"] + "," + foundRows[0]["Line"]);
  50.                         Console.WriteLine("row 1:");
  51.                         Console.WriteLine(foundRows[1]["Code"] + "," + foundRows[1]["Open"] + "," + foundRows[1]["High"] + "," + foundRows[1]["Low"] + "," + foundRows[1]["Close"] + "," + foundRows[1]["Change"] + "," + foundRows[1]["Trade"] + "," + foundRows[1]["Volume"] + "," + foundRows[1]["Value"] + "," + foundRows[1]["Line"]);
  52.                         Console.WriteLine("===================");
  53.                     }
  54.                 }
  55.             }
  56.         }
  57.     }
  58. }