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~