Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you are doing much lookup and speed is more important than memory you might want to process the file once and put the information into a dictionary. That way lookup is very fast and you only have to read the file once.</p> <p>Here's some code that will parse data like the example you gave:</p> <pre><code>class Program { static void Main(string[] args) { string filename = "example.txt"; Dictionary&lt;string, int[][]&gt; myDictionary = new Dictionary&lt;string, int[][]&gt;(); BuildMyDataDictionary(filename, myDictionary); //lookup via key int x = 20; int y = 180; string key = string.Format("{0}.{1}", x, y); int[][] values = myDictionary[key]; //print the values to check foreach (int[] array in values) foreach (int i in array) Console.Write(i + ", "); Console.WriteLine(); Console.ReadKey(); } private static void BuildMyDataDictionary(string filename, Dictionary&lt;string, int[][]&gt; myDictionary) { using (StreamReader r = new StreamReader(filename)) { string line = r.ReadLine(); // read through the file line by line and build the dictionary while (line != null) { Regex regx = new Regex(@"//\s*H\-(\d*)\w(\d*)"); Match m = regx.Match(line); if (m.Success) { // make a key of the two parts int 1 and int2 separated by a "." string key = string.Format("{0}.{1}", m.Groups[1], m.Groups[2]); // continue reading the block List&lt;int[]&gt; intList = new List&lt;int[]&gt;(); line = r.ReadLine(); while (!Regex.IsMatch(line, @"^\s*\}")) { Regex regex = new Regex("[{},]"); intList.Add(regex.Replace(line, " ").Trim().Split(new char[] { ' ' }).Select(int.Parse).ToArray()); line = r.ReadLine(); } myDictionary.Add(key, intList.ToArray()); } line = r.ReadLine(); } } } } </code></pre> <p>The example file I tested with was:</p> <pre><code>{ // H-20e180a.wav {-7,5,-4,-1,-9,2,-5,-1,2,-12,12,-33,34,-48,41,-40,16,20,730,4751,3861}, {-7,5,-4,-1,-9,2,-5,-1,2,-12,12,-33,34,-48,41,-40,16,20,} } { // H-21e181a.wav {-7,5,-4,-1,-9,2,-5,-1,2,-12,12,-33,34,-48,41,-40,16,20,730,4751,3861}, {-7,5,-4,-1,-9,2,-5,-1,2,-12,12,-33,34,-48,41,-40,16,20,} {-7,5,-4,-1,-9,2,-5,-1,2,-12,12,-33,34,-48,41,-40,16,20,730,4751,3861}, } </code></pre> <p>I borrowed the int[] parsing and creation from jltrem above.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload