Checking a word against a wordlist / Hashtables

I first tried using an Array (just because I knew how to do it), but this didn’t go well at all. From what I gather, using a Hashtable is the best way to go about this - is that correct?

If so, does anybody have an example / link /advice they could show me that explains how to go about setting this up in Unity? I’ve followed the link to the Microsoft page via the Unity docs, but without a Unity/JS example to follow I didn’t really know where to start with it.

I have a text file with one word per line (~75k words), though I can edit this so that it has any necessary code around each word if that makes it easier. I don’t need to do anything too fancy with this, just check whether a given word is in the list or not.

Thanks all

I think you’d be better off using ArrayList, which has the Contains method.

var myList = new ArrayList();
...
if (myList.Contains("something"))...

–Eric

Hashtables also have a contains method:

var mywords = {"apple":"fruit", "monkey":"animal"};

if (mywords.ContainsKey("apple")) Debug.Log("Apple found!");
if (mywords.ContainsValue("animal")) Debug.Log("Animal found!");

But an ArrayList does allow you to access elements by index (rather than key only) and perform Sort() operations.

I’ve been playing around with ArrayLists as suggested and seem to be on the right track.

I take it that populating the list with .Add on start up is the only way to create the list? I tried using “var testlist = new ArrayList (“a”, “b”, “c”);” like an array, but it returns an error (no visible constructor iirc). I don’t forsee this .Add method being a problem, just want to double-check that I’m not missing something obvious!

Thank you both for your help.

I think you have to use Add() to populate it. There is also an AddRange(), but I haven’t tried it.

I’m curious though, why do you need a Hashtable or ArrayList for a word list? Do you really need a key value pair? Are you grouping them by category?

I can use .Contains on an ArrayList, but not an Array (this may vary in other languages, I couldn’t say). With an Array I had to loop through entry by entry (which just crashed Unity under the strain).

Actually you can do “var testlist = new ArrayList ( [“a”, “b”, “c”] );”

–Eric

Built in .NET arrays have a Find() and an Exists() method.

I haven’t tested them, but they may do what you need. The .NET arrays are probably going to be the fastest way to deal with a large word list.

:sweat_smile:

Forget all I said about key value pairs!

I was getting SortedList confused with ArrayList.

SortedList is like a hashtable with more features, an ArrayList is like a resizable .NET array.

Perfect, thanks.