MathGroup Archive 2011

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

Search the Archive

Question about function construction


Hi, there.
  I want to construct a function serving as a hash table. This is how I make it:

  ConstructHashtable[] := 
   Module[{pairs = {{"key1", "value1"}, {"key2", "value2"}, {"key3", 
        "value3"}, {"key4", "value4"}}}, Clear[Hashtable]; 
    Hashtable[key_?StringQ] := (Hashtable[#[[1]]] = #[[2]]) & /@ pairs;]

And then I run the code above together with a test:

  ConstructHashtable[]; Hashtable["key1"]

Yet it returns all values:

  {"value1", "value2", "value3", "value4"}

But if I run it again:

  Hashtable["key1"]

It will get normal:

  "value1"

What is the reason for this? Thank you~


  • Prev by Date: VertexAdd seems to forget EdgeWeight values
  • Next by Date: plotting contours on a sphere
  • Previous by thread: VertexAdd seems to forget EdgeWeight values
  • Next by thread: Re: Question about function construction