开发者

My hashtable doesnt work

开发者 https://www.devze.com 2023-02-07 18:20 出处:网络
I am using a hashtable to read data from file and make clusters. Say the data in file is: umair,i,umair

I am using a hashtable to read data from file and make clusters.

Say the data in file is:

umair,i,umair
sajid,mark,i , k , i

The output is like:

[{umair,umair},i]
[sajid,mark,i,i,k]

But my code does not work. Here is the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;
namespace readstringfromfile
{

    class Program
    {
        static void Main()
        {
            /* int i = 0;
             foreach (string line in File.ReadAllLines("newfile.txt"))
             {
                 string[] parts = line.Split(',');
         开发者_StackOverflow社区        foreach (string part in parts)
                 {
                     Console.WriteLine("{0}:{1}", i,part);
                 }
                 i++; // For demo only
             }*/
            Hashtable hashtable = new Hashtable();

            using (StreamReader r = new StreamReader("newfile.txt"))
            {
                string line;
                while ((line = r.ReadLine()) != null)
                {
                    string[] records = line.Split(',');
                    foreach (string record in records)
                    {
                        if (hashtable[records] == null)
                            hashtable[records] = (int)0;

                        hashtable[records] = (int)hashtable[records] + 1;
                        Console.WriteLine(hashtable.Keys);

                    }
/////this portion is not working/////////////////////////////////////

                    foreach (DictionaryEntry entry in hashtable)
                    {
                        for (int i = 0; i < (int)hashtable[records]; i++)
                        {
                            Console.WriteLine(entry);
                        }
                    }
                }
            }
        }
    }
}


You're working with the records array when inserting into the hashtable (and when reading from it) instead of using the foreach-variable record. Also, in the final look, you iterate based on records instead of the current entry.Key. You're also declaring the hashtable in a too wide scope, causing all rows to be inserted into the same hashtable, instead of one per row.

public static void Main() {
    var lines = new[] { "umair,i,umair", "sajid,mark,i,k,i" };
    foreach (var line in lines) {
        var hashtable = new Hashtable();
        var records = line.Split(',');

        foreach (var record in records) {
            if (hashtable[record] == null)
                hashtable[record] = 0;

            hashtable[record] = (Int32)hashtable[record] + 1;
        }

        var str = "";
        foreach (DictionaryEntry entry in hashtable) {
            var count = (Int32)hashtable[entry.Key];
            for (var i = 0; i < count; i++) {
                str += entry.Key;
                if (i < count - 1)
                    str += ",";
            }
            str += ",";
        }

        // Remove last comma.
        str = str.TrimEnd(',');

        Console.WriteLine(str);
    }

    Console.ReadLine();
}

However, you should consider using the generic Dictionary<TKey,TValue> class, and use a StringBuilder if you're building alot of strings.

public static void Main() {
    var lines = new[] { "umair,i,umair", "sajid,mark,i,k,i" };
    foreach (var line in lines) {
        var dictionary = new Dictionary<String, Int32>();
        var records = line.Split(',');

        foreach (var record in records) {
            if (!dictionary.ContainsKey(record))
                dictionary.Add(record, 1);
            else
                dictionary[record]++;
        }

        var str = "";
        foreach (var entry in dictionary) {
            for (var i = 0; i < entry.Value; i++) {
                str += entry.Key;
                if (i < entry.Value - 1)
                    str += ",";
            }
            str += ",";
        }

        // Remove last comma.
        str = str.TrimEnd(',');

        Console.WriteLine(str);
    }

    Console.ReadLine();
}


You're attempting to group elements of a sequence. LINQ has a built-in operator for that; it's used as group ... by ... into ... or the equivalent method .GroupBy(...)

That means you can write your code (excluding File I/O etc.) as:

var lines = new[] { "umair,i,umair", "sajid,mark,i,k,i" };
foreach (var line in lines) {
    var groupedRecords =
        from record in line.Split(',')
        group record by record into recordgroup
        from record in recordgroup
        select record;

    Console.WriteLine(
        string.Join(
            ",", groupedRecords
        )
    );
}

If you prefer shorter code, the loop be equivalently written as:

foreach (var line in lines) 
    Console.WriteLine(string.Join(",",
        line.Split(',').GroupBy(rec=>rec).SelectMany(grp=>grp)));

both versions will output...

umair,umair,i
sajid,mark,i,i,k

Note that you really shouldn't be using a Hashtable - that's just a type-unsafe slow version of Dictionary for almost all purposes. Also, the output example you mention includes [] and {} characters - but you didn't specify how or whether they're supposed to be included, so I left those out.

A LINQ group is nothing more than a sequence of elements (here, identical strings) with a Key (here a string). Calling GroupBy thus transforms the sequence of records into a sequence of groups. However, you want to simply concatenate those groups. SelectMany is such a concatenation: from a sequence of items, it concatenates the "contents" of each item into one large sequence.

0

精彩评论

暂无评论...
验证码 换一张
取 消