Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #2955

Question about function construction

From ZiYuan Lin <ziyuang@gmail.com>
Newsgroups comp.soft-sys.math.mathematica
Subject Question about function construction
Date 2011-06-05 11:04 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <isfnrv$btm$1@smc.vnet.net> (permalink)

Show all headers | View raw


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~

Back to comp.soft-sys.math.mathematica | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Question about function construction ZiYuan Lin <ziyuang@gmail.com> - 2011-06-05 11:04 +0000
  Re: Question about function construction Albert Retey <awnl@gmx-topmail.de> - 2011-06-06 10:23 +0000

csiph-web