# Is there a method to enumerate the keys/values of SystemUtilitiesHashTable

Lacking from the simple API of SystemUtilitiesHashTable is a method to enumerate the keys/values.

?SystemUtilitiesHash*



After creating a hash table

h = SystemUtilitiesHashTable[];
SystemUtilitiesHashTableAdd[h, "a", 1];


Various methods to extract its content fail.

h[[1]]
h
DownValues[h]


Any way out?

-
I wonder what HashTableGet[] does... :) –  Guess who it is. Jun 8 '13 at 15:17
@0x4A4D I think OP wants to list all available key-value pairs in the table, which I believe is not possible. –  The Toad Jun 8 '13 at 15:21
If you want to be able to enumerate the contents, you're probably better off just to use ordinary downvalues rather than the SystemUtilitiesHashTable. –  Oleksandr R. Jun 8 '13 at 19:44
Just to follow up on 0x4A4D, you have tried SystemUtilitiesHashTableGet[h, "a"]? –  Seth Chandler Jun 9 '13 at 5:35

I'm sure there must be a better way, but this works:

h = SystemUtilitiesHashTable[];
SystemUtilitiesHashTableAdd[h, "a", 1];
SystemUtilitiesHashTableAdd[h, "b", 2];

ToExpression[ToString[h, InputForm], InputForm, Hold][[1, 2]]

{{"a", 1}, {"b", 2}}


Mr. Wizard provides a more terse version:

ToHeldExpression[ToString[h, InputForm]][[1, 2]]

-
Unless there is something like SparseArray[. . .]["NonzeroPositions"] for HashTable there very well may not be a better way. –  Mr.Wizard Jun 9 '13 at 13:48
More terse: ToHeldExpression[ToString[h, InputForm]][[1, 2]] –  Mr.Wizard Jun 9 '13 at 14:55
As with many things Mathematica I learned it from Leonid. It's an old function but still entirely, well, functional. –  Mr.Wizard Jun 10 '13 at 10:13
+1. I still hope there is some more robust way though, since ToString-ToExpression transformation is not always robust. –  Leonid Shifrin Jun 10 '13 at 10:28
@Leonid for testing purposes can you give an example where Simon's method fails? –  Mr.Wizard Jun 10 '13 at 12:01