- Home /
Javascript TextAsset to Generic Dictionary
I have read around and it seems the best way to use a text file like a hashtable is with a generic dictionary. Of course, easier said than done. There are plenty of examples on parsing text files, but none on passing them into a dictionary that I have found.
I have cobbled together some code from the limited information/examples that exist on Javascript dictionaries. It doesn't work , but I'm not sure why. I can get the code to compile without errors, so I assume the syntax is OK(if conceptually incorrect/incomplete), but it fails to find the key/value pairs in the dictionary. Error message is
KeyNotFoundException: The given key was not present in the dictionary.
I suspect there might be two errors: one in how I understand splitting the text file into arrays, and another in how I understand adding/fetching key/value pairs in a dictionary.
I am a novice, so please excuse some of my ignorance. Any advice would be appreciated. Code is below:
Text file is just this:
NUMA*One
NUMB*Two
NUMC*Three
NUMD*4
NUME*Five
NUMF*Six
NUMG*7
NUMH*Eight
Code attached to GameObject is:
#pragma strict
import System.Collections.Generic;
/* Load the text file */
var textFile : TextAsset;
/* Create an dictionary for the values in the file */
var textInfo = new Dictionary.<String,String>();
function Start () {
/* split the text file up by newline characters */
var textLines : String [] = textFile.text.Split("/n"[0]);
/* loop through each line in the file */
for ( var thisLine : String in textLines ) {
/* split each line by asterisks into array of two variables*/
var valueString : String [] = thisLine.Split("*"[0]);
/* add the two variables in the array into the dictionary as a key and value*/
textInfo.Add(valueString[0], valueString[1]);
}
}
function Update () {
}
function OnGUI () {
/* make a labelled box to show a string pulled from dictionary by key*/
GUI.Box (Rect (10,10,80,80), textInfo["NUMB"]);
}
Thanks!
Thanks for posting your code dav78! I found it really useful so gave you a thumbs up. :)
Answer by Eric5h5 · Jun 18, 2013 at 03:15 AM
You can do basic debugging using Debug.Log to see what the actual results of your code are compared to what you might expect. Examples are
Debug.Log (textLines.Length);
Debug.Log (thisLine);
Debug.Log (valueString[0] + " " + valueString[1]);
In this case the result of the first one (that the length of the array is 1) indicates that something fundamental is going wrong. A closer look reveals that in fact you are splitting on the "/" character, which doesn't occur in your data file. To indicate a newline character, use "\n"
.
Haha, thanks so much! I have mixed feelings. On the one hand, I'm happy my basic code structure was correct. On the other hand, it is such a stupid $$anonymous$$or mistake of one character. Anyway, your point about debugging is well made. I should have gone to Debug first. A valuable lesson. Nonetheless, thanks for your time!
it is such a stupid $$anonymous$$or mistake of one character
Welcome to program$$anonymous$$g. ;)
Your answer
Follow this Question
Related Questions
Best Way to Represent an Inventory? 0 Answers
importing english dictionary as text file? 1 Answer
How to get the keys of a SimpleJSON dictionary ? 3 Answers
GUIText Help 1 Answer
UI Text showing weird string value? 1 Answer