I am looking for a way to look up values โ€‹โ€‹in a .net hash table using wildcards

I have a whole bunch of values โ€‹โ€‹stored in a .net 2.0 hash table. What I really would like to find is a way to essentially make a SQL select statement on a table.

Meaning, I would like to get a list of keys whose associated values โ€‹โ€‹match a very simple text pattern (along the lines "starts with a number".)

The ultimate goal will be to remove these records from the hash table for further processing.

I have already fought back a little from this, and I can not think of anything.

Any ideas?

(In case of coincidence, this matters: due to the realities of this project, any third-party widgets or updates to a later .net version are disconnected from the table.)

+1


source to share


3 answers


You can use regex for every key in the hash table. It's very messy, but it works:

    static void Main(string[] args)
    {
        Hashtable myhashtable = new Hashtable();
        myhashtable.Add("Teststring", "Hello");
        myhashtable.Add("1TestString1", "World");
        myhashtable.Add("2TestString2", "Test");

        List<String> newht = new List<String>;

        //match all strings with a number at the front
        Regex rx = new Regex("^[1-9]");
        foreach (string key in myhashtable.Keys)
        {
            if (rx.IsMatch(key) == true)
            {
                newht.Add(key);
            }
        }

        //Loop through all the keys in the new collection and remove them from
        //them from the main hashtable.
        foreach (string key in newht)
        {
            myhashtable.Remove(key);
        }
    }

      

EDIT: And just for fun, here's the LINQ version (sorry I'm just too).



            Hashtable myhashtable = new Hashtable();
            myhashtable.Add("Teststring", "Hello");
            myhashtable.Add("1TestString1", "World");
            myhashtable.Add("2TestString2", "Test");

            Regex rx = new Regex("^[1-9]");
            var k = (from string key in myhashtable.Keys
                     where rx.IsMatch(key)
                     select key).ToList();

            k.ForEach(s => myhashtable.Remove(s));

      

EDIT: I just added a sting list, not a hash table, I couldn't remember which version of .net had shared lists in it. *** slaps thebhead

+2


source


If you are really looking for things that start with a number, then you can do it much faster than using Regex. Just look at the first character of each key and determine if it is a number. Save the keys that you want to remove in the list, as all you have to do is save the key.



    List<string> keysToRemove = new List<string>( myhashtable.Count );
    foreach (string key in myhashtable.Keys)
    {
        if (char.IsDigit(key[0])
        {
            keysToRemove.Add(key);
        }
    }

    foreach (string key in keysToRemove)
    {
        myhashtable.Remove(key);
    }

      

+3


source


Using LINQ:

Dim myhashtable As New Hashtable
    myhashtable.Add("Teststring", "Hello")
    myhashtable.Add("1TestString1", "World")
    myhashtable.Add("2TestString2", "Test")

For Each i As String In From Element In myhashtable.Cast(Of DictionaryEntry)() Let k = DirectCast(Element.Value, String) Where k.StartsWith("W") Select DirectCast(Element.Key, String)
        MsgBox("This key has a matching value:" & i)
    Next

      

But it is better to use a dictionary if using LINQ:

    Dim d = New Dictionary(Of String, String)()
    d.Add("Teststring", "Hello")
    d.Add("1TestString1", "World")
    d.Add("2TestString2", "Test")

    For Each i As String In From element In d Where element.Value.StartsWith("W") Select element.Key
        MsgBox("This key has a matching value:" & i)
    Next

      

And instead of .StartsWith ("W") you can of course do any other filtering you want.

+1


source







All Articles