- Home /
Load txt file (CSV) into Generic Dictionary Javascript
currentlyl I'm loading a csv into an array. I want to be able to search it for a string so I think I should use a generic dictionary as it will be faster - the array is quite large.
My current code is:
var wordCSV : TextAsset;
function Start ()
{
//Load Dictionary
var wordList = wordCSV.text.Split(";"[0]);
}
function FindWord(word : String)
{
for(int i = 0; i < array.Length; i++)
{
if(array[i].name == word)
{
print("I am element number " + i );
}
}
}
How can I do the same with a Generic Dictionary?
Answer by Eric5h5 · Feb 11, 2013 at 08:09 PM
You wouldn't use a Dictionary, since that's for key/value pairs. If all you want to do is tell if a collection contains a word, then you'd use a HashSet.
var wordSet = new HashSet.< String >(wordList);
if (wordSet.Contains("cat")) {
Debug.Log ("Yep.");
}
Thanks for the answer, is HashSet different from Hashtable?
I'm getting an error: Assets/Scripts/WordScript.js(9,36): BCE0043: Unexpected token: (.
from this code: var wordList = new HashSet.(wordCSV);
I couldn't find any reference to HashSet in the script reference
Sorry, the code formatting messed that up. Edited. Yes a HashSet is different than a HashTable, which is like a Dictionary (except slower/not type-safe, so there's no real reason to use a HashTable when you can use Dictionary ins$$anonymous$$d).
Also you can't use "`new HashSet.(wordCSV)`", assu$$anonymous$$g wordCSV is your TextAsset. If you're not setting it up manually, it needs to be initialized from an array.
hmm. I'm getting an error: Assets/Scripts/WordScript.js(10,28): BCE0005: $$anonymous$$ identifier: 'HashSet'.
Your answer
![](https://koobas.hobune.stream/wayback/20220613092531im_/https://answers.unity.com/themes/thub/images/avi.jpg)