- Home /
Getting a key value from a hashtable?
Hey guys,
I've got some data in a hashtable(int, array) and I need to be able to loop through all the keys and get it's integer value as it's dynamic and not known.
Basically I'm pulling data from a mysql database through a WWW to a .php script, parsing the returned string into an array and using the unique row ID from the database as the key. I need to be able to loop through all the keys and put them into another array as the appropriate integers.
When I try something like this:
var keys = new Array();
for (i in myHashTable) {
keys.push(i);
}
I get an array full of these "System.Collections.DictionaryEntry" and not the integer (key) I'm looking for.
Answer by perchik · Jul 15, 2013 at 05:00 PM
How is your hashtable created? If you're using C# you can access myHashTable.Keys which does exactly what you want.
foreach(int key in myHashtable.Keys){
Debug.Log("key:" + key + " value: "+myHashTable[key]);
}
Sorry I should have mentioned that, it's using Javascript and I'm using this:
var split = unparsed.Split(","[0]);
for (i in split) {
var attributes = i.Split("|"[0]);
if (attributes[0] != "") {
myHashTable.Add(parseInt(attributes[0]),attributes);
}
}
I've also tried this with no luck:
var keys = new Array();
for(var k in myHashTable) keys.push(k);
for (i in keys) print(myHashTable[i][1]);
I get: "NullReferenceException: Object reference not set to an instance of an object"
Your answer
![](https://koobas.hobune.stream/wayback/20220613110144im_/https://answers.unity.com/themes/thub/images/avi.jpg)