Is there a way to find only part of the key value in a dictionary object? - vb.net

There a dictionary object that gets loaded with the following key values:
119
189a
189b
189c
197
201a
201b
In most situations, life is good and all the individual key values are needed/unique. But in certain situations, the keys with letters behind them (ie...189a, 189b, 189c) all mean the same thing (ie...189). So I need a way to see if a key value exists (like the containskey method) for only the first part of the key and then return true.
Any ideas on how to accomplish this?

Since you only sometimes need ignore the suffixed letter, for the greatest efficiency, I would recommend using an additional HashSet(T) to store the numeric portion. When you add/remove elements from your dictionary, also add/remove the numeric from the HashSet(T). HashSet(T).Contains method is O(1), so checking to see if an element exists will be quick.

Something like this?
dictionary.Keys.Any(Function(key) key.StartsWith("189"))
or you can use a Regex for more find-grained control:
dictionary.Keys.Any(Function(key) Regex.IsMatch(key, "^189[^\d]?")

Related

String Template: is it possible to get the n-th element of a Java List in the template?

In String Template one can easily get an element of a Java Map within the template.
Is it possible to get the n-th element of an array in a similar way?
According to the String Template Cheat Sheet you can easily get the first or second element:
You can combine operations to say things like first(rest(names)) to get second element.
but it doesn't seem possible to get the n-th element easily. I usually transform my list into a map with list indexes as keys and do something like
map.("25")
Is there some easier/more straightforward way?
Sorry, there is no mechanism to get a[i].
There is no easy way getting n-th element of the list.
In my opinion this indicates that your view and business logic are not separated enough: knowledge of what magic number 25 means is spread in both tiers.
One possible solution might be converting list of values to object which provides meaning to the elements. For example, lets say list of String represents address lines, in which case instead of map.("3") you would write address.street.

Enumerating Strings as bytes?

I was looking for a way to enumerate String types in (vb).NET, but .NET enums only accept numeric type values.
The first alternative I came across was to create a dictionary of my enum values and the string I want to return. This worked, but was hard to maintain because if you changed the enum you would have to remember to also change the dictionary.
The second alternative was to set field attributes on each enum member, and retrieve it using reflection. Surely enough this worked aswell and also solved the maintenance problem, but it uses reflection and I've always read that using reflection should be a last resort thing.
So I started thinking and I came up with this: every ASCII character can be represented as a hexadecimal value, and you can assign hexadecimal values to enum members.
You could get rid of the attributes, assign the hexadecimal values to the enum members. Then, when you need the text value, convert the value to a byte array and use System.Text.Encodings.ASCII.GetString(enumMemberBytes) to get the string value.
Now speaking out of experience, anything I come up with is usually either flawed or just plain wrong. What do you guys think about this approach? Is there any reason not to do it like that?
Thanks.
EDIT
As pointed out by David W, enum member values are limited in length, depending on the underlying type (integer by default). So yes, I believe my method works but you are limited to characters in the ASCII table, with a maximum length of 4 or 8 characters using integers or longs respectively.
The easiest way I have found to dynamically parse a String representation of an Enumeration into the actual Enumeration type was to do the following:
Private EnumObject
[Undefined]
ValueA
ValueB
End Enum
dim enumVal as EnumObject = DirectCast([Enum].Parse(GetType(EnumObject), "ValueA"), EnumObject)
This removes the need to maintain a dictionary and allows you to just handle strings instead of converting to an Int or a Long. This does use reflection, but I have not come across any issues as long as you catch and handle any exceptions with the String Parse.

How to view, find and remove key value pairs in CFMutable Dictionary

How do i find and remove the key value pairs of a particular key using CFMutableDictionaryRef.
I have added a value using CFMutableDictionary but i need to know how to search ,view and delete a keyvalue pair.
The value i have created is a structure pointer and key is an integer value.
Beata,
The CFMutableDictionaryRef documentation shown Here will guide you.
In the order of your question:
For finding an element, see CFDictionaryGetValue
For removing an element, see CFDictionaryRemoveValue
Note that the CFDictionary types are a 'toll-free-bridge' with NSDictionary.
Frank

How to cut the size of my ArrayList?

Lets say my ArrayList got 20 elements. Well, sometime in runtime I will want it to only have 10 elements, so the other 10 indexes(from 9 to 19) don't exist at all. How do I do that?
Why are you using non-generic collections at all? Use List<T> instead. And you can use the Remove() method on it.
Since your working with ArrayLists, and not Arrays, just use ArrayList.Remove() for a single part, and ArrayList.RemoveRange(x, list.Count-x) for a range.
If you're looking to avoid using memory space for the elements that are not present, you can try with the TrimToSize method:
http://msdn.microsoft.com/en-us/library/system.collections.arraylist.trimtosize.aspx
It will minimize the array overhead, by limiting the capacity to the current items.
If they already exist, you'll need to remove them and then, call this method.
I'm not sure what you're looking for exactly, so more details would be great.
If you just want to remove the elements without changing the capacity, you can use ArrayList.RemoveRange:
http://msdn.microsoft.com/en-us/library/system.collections.arraylist.removerange.aspx
arrayList.RemoveRange(10, list.Count-10)
This method can be used with any other valid range, if you e.g. want to delete the first ten elements instead.

Extract terms from query for highlighting

I'm extracting terms from the query calling ExtractTerms() on the Query object that I get as the result of QueryParser.Parse(). I get a HashTable, but each item present as:
Key - term:term
Value - term:term
Why are the key and the value the same? And more why is term value duplicated and separated by colon?
Do highlighters only insert tags or to do anything else? I want not only to get text fragments but to highlight the source text (it's big enough). I try to get terms and by offsets to insert tags by hand. But I worry if this is the right solution.
I think the answer to this question may help.
It is because .Net 2.0 doesnt have an equivalent to java's HashSet. The conversion to .Net uses Hashtables with the same value in key/value. The colon you see is just the result of Term.ToString(), a Term is a fieldname + the term text, your field name is probably "term".
To highlight an entire document using the Highlighter contrib, use the NullFragmenter