MathGroup Archive 1998

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: keys in a hash table (aka, all assigned indices in an array): awful hack


> 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


  • Prev by Date: Interpolated animation
  • Next by Date: pasting the outputs
  • Previous by thread: keys in a hash table (aka, all assigned indices in an array): awful hack
  • Next by thread: Re: keys in a hash table (aka, all assigned indices in an array): awful hack