Re: keys in a hash table (aka, all assigned indices in an array): awful hack
- To: mathgroup at smc.vnet.net
- Subject: [mg13339] Re: [mg13281] keys in a hash table (aka, all assigned indices in an array): awful hack
- From: David Withoff <withoff>
- Date: Mon, 20 Jul 1998 02:49:57 -0400
- Sender: owner-wri-mathgroup at wolfram.com
> suppose I make some assignments like this:
> fruits[apple]= crunchy
> fruits[lemon]= sour
>
> I'm treating fruits like a hash table and now I would like a list of all
> the keys. In this case {apple,lemon}.
>
> It seems like there should be an easy way to do this. All I came up with
> was the following hack:
>
> keys[hash_]:=
> DownValues[hash] /. {RuleDelayed->(#&), hash->(#&)} //ReleaseHold
>
> the hash values will be simple; we can just do fruits/@keys[fruits]
>
> Thanks,
> Daniel
>
> --
> Daniel Reeves dreeves at umich.edu http://interlabs.bradley.edu/~daniel
>
> "Computer Science is no more about computers than astronomy is about
> telescopes." -- E W Dijkstra
I have done this using
keys[hash_]:=Map[#[[1,1,1]]&,DownValues[hash]]
but the basic idea is the same. This looks ok to me. I guess whether
or not this is a hack is in the eye of the beholder.
Dave Withoff
Wolfram Research