Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2011

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

Search the Archive

Question about function construction

  • To: mathgroup at smc.vnet.net
  • Subject: [mg119474] Question about function construction
  • From: ZiYuan Lin <ziyuang at gmail.com>
  • Date: Sun, 5 Jun 2011 07:04:29 -0400 (EDT)
  • Reply-to: comp.soft-sys.math.mathematica at googlegroups.com

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