2012-04-27 20 views
0

我想讀取一個文件到一個全局數組中,循環訪問數組,並確定每個字符串對應的數量,然後在自己的組中找到總和。並將這些組分別按自己的唯一編號分組。分解二維數組的最有效方法?

,我在我閱讀的文本文件:

Name,50 
Name,40 
DifferentName,50 
AnotherName,10 
Name,60 

什麼是分裂的逗號行的最簡單的方法,使數字對應的具體名稱,不知道哪個順序,他們將在嗎?

這裏是我到目前爲止使用的代碼。儘管現在它只是打開的文件對話框,我把它作爲一個建設性的參考。

 string strFileName; 

    private void btnReadInFile_Click(object sender, EventArgs e) 
    { 
     //select the file 
     OpenFileDialog ofdGetFile = new OpenFileDialog(); 
     if (ofdGetFile.ShowDialog() == DialogResult.Cancel) 
     { 
      //cancel 
      MessageBox.Show("User Canceled"); 
      return; 
     } 
     else 
     { 
      //open the file 
      StreamReader myStreamReader; 
      strFileName = ofdGetFile.FileName; 
      FileStream input = new FileStream(strFileName, FileMode.Open, FileAccess.Read); 
      myStreamReader = new StreamReader(input); 
      // other 
      MessageBox.Show("Reading Complete", "Done!", MessageBoxButtons.OK, MessageBoxIcon.Information); 
     } 
    } 

    private void btnShowGrade_Click(object sender, EventArgs e) 
    { 
     if (strFileName != null) 
     { 
      String[][] nameSums = System.IO.File.ReadLines(strFileName) 
      .Select(l => l.Split(','))    // split the line 
      .GroupBy(arr => arr[0])     // group by name 
      .Select(grp => new String[]{ 
      grp.Key, 
      grp.Sum(arr => int.Parse(arr[1])).ToString()}) 
      .ToArray(); 

     } 
     else 
     { 
      MessageBox.Show("You need to read in a file first."); 
     } 
    } 
} 

我覺得必須有更好的方法來做到這一點。

迄今爲止,非常感謝您的幫助!我相信這個問題沒有解決的唯一原因是我缺乏溝通技巧。

回答

0
myArray = File.ReadAllLines(myFileName).Select(l => l.Split(',')).ToArray(); 

注意這將是交錯排列,而不是二維排列。創建一個二維的,你可以使用

lines = File.ReadAllLines(myFileName).Select(l => l.Split(',')).ToArray(); 
myArray = new string[lines.Count(), 2]; 
for(int i = 0; i < lines.Length; i++) 
{ 
    myArray[i, 0] = lines[i, 0]; 
    myArray[i, 1] = lines[i, 1]; 
} 
0

爲什麼不使用字典?

所以,

Dictionary<string,int> namesAndAges = new Dictionary<string,int>(); 
String[] line = new String[2]; 
while(there's still xml to read) 
    { 
     aLineOfXml = blah; //read a line of xml however you were gonna do it 
     line = aLineOfXml.Split(','); 
     namesAndAges.Add(line[0],line[1]); 
    } 

前兩行的while循環將得到更好的凝聚成一個,但是爲了更因爲我不想添加任何XML解析的代碼,我把它分解一樣, 。

0

您的任務是IO綁定。更改代碼很可能不會獲得任何好處。首先,您應該使用分析器來查找應用程序的瓶頸。

如果您有.Net 4,4.5或Silverlight使用ReadLines方法。它返回一個表示線的集合的迭代器。摘錄:

 List<KeyValuePair<string, double> values = new List<KeyValuePair<string, double>(); 
     var lines = File.ReadLines(myFile); 
     foreach (var line in lines) 
     { 
      var data = line.Split(','); 
      var x = data[0]; 
      var y = Double.Parse(data[1], CultureInfo.InvariantCulture); 
      var pair = new KeyValuePair<string, double>(x, y); 
      values .Add(pair); 
     } 
1

編輯根據上次編輯:

識別每個串到它的相應金額,然後找到資金在自己的羣體

它仍然不是清楚你想達到什麼。你應該提供你想要的樣本數據結果。我假設金額是數字,組是名稱組。

所以對於Name -group這將是50+40+60=150DifferentName = 50AnotherName = 10

這創建唯一的名稱的詞典和它們根據量:

Dictionary<String, int> nameSums = 
     System.IO.File.ReadLines(path) // read lines from file 
    .Select(l => l.Split(','))  // split the line 
    .GroupBy(arr => arr[0])   // group by name 
    .ToDictionary(grp => grp.Key, grp => grp.Sum(arr => int.Parse(arr[1]))); 

如果硬要陣列的方法,以下創建並初始化交錯數組(數組的數組)的名稱作爲第一元件和的總和作爲第二:

String[][] nameSums = System.IO.File.ReadLines(path) // read lines from file 
      .Select(l => l.Split(','))    // split the line 
      .GroupBy(arr => arr[0])     // group by name 
      .Select(grp => new String[]{ 
       grp.Key, 
       grp.Sum(arr => int.Parse(arr[1])).ToString()}) 
      .ToArray(); 

第一的方法:

「讓數字對應的具體名稱」

所以,我認爲你要屬於每個數字的名字。如果這是真的一個Dictionary<String, List<String>>將是最好的選擇:

var numNames = System.IO.File.ReadLines(path) 
    .Select(l => l.Split(',')) 
    .GroupBy(arr => arr[1]) 
    .ToDictionary(grp => grp.Key, grp => grp.Select(arr => arr[0]).ToList()); 
  1. 該行從文件中讀取作爲IEnumerable<String>
  2. 則每行是由逗號分割的String[]
  3. 這些由分組第二個元素(數字)得到唯一的數字和他們的名字
  4. 結果將用於創建一個Dictionary,(現在)唯一的數字是鍵和名稱的值爲List<String>

如果你不熟悉dictionaries,你通常通過密鑰訪問它們,所以如果你想知道的數量"50"所有名稱:

List<String> names = numNames["50"]; 
foreach(String name in names) 
    Console.Write("name={0}", name); 

或者,如果你想要遍歷所有:

foreach(var numName in numNames) 
    foreach(var name in numName.Value) 
     Console.Write("number={0} name={1}", numName.Key, name); 
+0

查看他的數據,都不是唯一的。 – 2012-04-27 19:23:02

+0

@YuriyFaktorovich:我的解釋是錯誤的。 OP的要求是:「讓數字符合他們的具體名稱」。所以我認爲他想要所有數字的名字。所以我按數字分組。 – 2012-04-27 19:55:44

+0

@YuriyFaktorovich:編輯我的答案,現在有希望更清楚。 – 2012-04-27 20:16:54

相關問題