Re: Is it possible to flatten hash table?
- To: mathgroup at smc.vnet.net
- Subject: [mg124174] Re: Is it possible to flatten hash table?
- From: "Oleksandr Rasputinov" <oleksandr_rasputinov at hmamail.com>
- Date: Wed, 11 Jan 2012 17:21:18 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <jejk6c$as0$1@smc.vnet.net>
On Wed, 11 Jan 2012 09:19:08 -0000, Szymon Roziewski
<szymon.roziewski at gmail.com> wrote:
> Hello,
> I would like to create a hash table for some data.
>
> MakeHash[lon_, lat_, depth_] := Module[{Arr},
> Arr[lon <> " " <> lat] = depth;
> Arr
> ];
>
> BathHashT =
> MakeHash[ToString[Rnd[#1, 6]], ToString[Rnd[#2, 6]], #3] & @@ # & /@
> Take[SeaBathCutEnd, 4];
>
> Res = Flatten@BathHashT;
>
> Res["-2.966667 -2.000000"]
>
> As a result of Flatten I get {Arr$2002701, Arr$2002702, Arr$2002703,
> Arr$2002704}["-2.966667 -2.000000"],
>
> The function Rnd is to round a float number to 6 decimal digits.
>
> So it seems to be 4 hash table. For each hash there is one table.
> I thought that it could be flatten to one hash containing all data.
> I could do it in for loop but due to its poor perfmormance I wanted to
> avoid that.
This is the result of Module, which creates a new symbol, Arr$<number>, on
every invocation. Your code is incomplete because Rnd and SeaBathCutEnd
are not defined so I am not quite sure what behaviour you see and what
result you are expecting; however, if you can be certain that there will
not be any collisions, you can re-use the same symbol:
MakeHash[lon_, lat_, depth_] := (Arr[lon <> " " <> lat] = depth; Arr)
To answer the question directly, it is possible to combine DownValues from
several symbols as follows:
inputs = MapAt[
ToString /@ # &,
RandomInteger[{1000, 2000}, {3, 10}],
{{1}, {2}}
];
hashes = MapThread[MakeHash, inputs];
DownValues[Arr] =
Flatten[DownValues /@ hashes] /.
Verbatim[HoldPattern][sym_[key__]] :>
HoldPattern[Arr[key]];
(* inputs[[All, 1]] == {"1796", "1556", 1486} *)
In :=
Arr["1796 1556"]
Out =
1486
However, I would not recommend this method over simply avoiding the
creation of multiple symbols in the first place.