Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2008

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

Search the Archive

Re: Datafunctions - multiple keys

  • To: mathgroup at smc.vnet.net
  • Subject: [mg89263] Re: Datafunctions - multiple keys
  • From: Mac <mwjdavidson at googlemail.com>
  • Date: Mon, 2 Jun 2008 04:31:07 -0400 (EDT)
  • References: <g1m8gv$j5$1@smc.vnet.net> <g1o8gn$hdt$1@smc.vnet.net>

Many thanks to Daniel for his input. Through some trial and error I
came up with a solution with the desired functionality myself which I
post for those interested in this functionality. With this
implementation you have

1) The ability to retrieve keys at level1

2) Given a level 1 key the ability to retrieve the keys at level2

3) Use Map and Apply to retrieve all keys and data as required for
your application (see example below)

A limitation of the implementation, however, is that it is limited to
only 2 keys. Maybe somebody has a more general implementation for an
arbitrary number of keys ?

In[1]:= Remove[f]
f["a", "a1"] := 1; f["a", "a2"] := 2; f["b", "b1"] := 3;
f["b", "b2"] := 4;
f[] := Union[Map[#[[1, 1, 1]] &, DownValues[f][[2 ;; -2]]]];
f[key1_] :=
 Cases[DownValues[f][[2 ;; -2]],
  HoldPattern[f[key1, k_]] -> {key1, k}, Infinity]
f[]
f["a"]
f["b"]
f[#] & /@ f[]
Apply[f, %, {2}]

Out[4]= {"a", "b"}

Out[5]= {{"a", "a1"}, {"a", "a2"}}

Out[6]= {{"b", "b1"}, {"b", "b2"}}

Out[7]= {{{"a", "a1"}, {"a", "a2"}}, {{"b", "b1"}, {"b", "b2"}}}

Out[8]= {{1, 2}, {3, 4}}

On May 30, 8:57 am, dh <d... at metrohm.ch> wrote:
> Hi Mac,
>
> a possible way to do this:
>
> store the data in a nested List. There is no need to distinguish between
>
> key and data, the data is the entry at the leaf.  Use functions that
>
> extract the needed part, if you specify the data as argument, you may
>
> even process multiple "data sets". Here is an example with 2 keys:
>
> dat={
>
> {x1,{x11,x12}},
>
> {x2,{x21,x22}},
>
> {x3,{}}
>
> }
>
> Remove[getDat];
>
> getDat[dat_] := dat;
>
> getDat[x_, dat_] := Cases[dat, {x, y_} -> y][[1]];
>
> getDat[x1_, x2_, dat_] := Cases[getDat[x1, dat], {x2, y2_} -> y2][[1]];
>
> and here a test:
>
> getDat[dat]
>
> getDat[x1, dat]
>
> getDat[x1, x12, dat]
>
> hope this helps, Daniel
>
>
>
> Mac wrote:
> > With some help from a previous post I've become quite proficient at
> > simple data functions and use them quite frequently. Here a simplified
> > example:
>
> > Clear[f]
> > f["a"] := 1; f["b"] := 2;
> > f[] := Map[#[[1, 1, 1]] &, DownValues[f][[2 ;; All]]];
>
> > then I use f[] to get a list of keys
>
> > f[]
> > {"a", "b"}
>
> > and retrieve the function data using for instance f["a"] to retrieve
> > 1. However I''m stumped as to how to extend this to more than one key.
> > There have been some posts in the past illustrating how to implement
> > multiple keys using a function of the form f[key1][key2], but I would
> > rather use something similar to the Wolfram data function
> > "ExampleData", where an empty call to example data, i.e. Example[] is
> > used to retrieve the high level categories and then
> > ExampleData["category"] is used to retrieve the examples within this
> > category. An example from my worksheet:
>
> > ExampleData[]
>
> > Out[91]= {"AerialImage", "Geometry3D", "LinearProgramming", "Matrix",
> > \
> > "Sound", "TestAnimation", "TestImage", "Text", "Texture"}
>
> > In[93]:= ExampleData["AerialImage"]
>
> > Out[93]= {{"AerialImage", "Earth"}, {"AerialImage",
> >   "FosterCity"}, {"AerialImage", "Oakland"}, {"AerialImage",
> >   "Oakland2"}, {"AerialImage", "Pentagon"}, {"AerialImage",
> >   "Richmond"}, {"AerialImage", "Richmond2"}, {"AerialImage",
> >   "SanDiego"}, {"AerialImage", "SanDiego2"}, {"AerialImage",
> >   "SanDiego3"}, {"AerialImage", "SanDiego4"}, {"AerialImage",
> >   "SanDiego5"}, {"AerialImage", "SanDiego6"}, {"AerialImage",
> >   "SanDiego7"}, {"AerialImage", "SanDiego8"}, {"AerialImage",
> >   "SanDiego9"}, {"AerialImage", "SanDiego10"}, {"AerialImage",
> >   "SanDiego11"}, {"AerialImage", "SanDiego12"}, {"AerialImage",
> >   "SanDiego13"}, {"AerialImage", "SanFrancisco"}, {"AerialImage",
> >   "SanFrancisco2"}, {"AerialImage", "SanFrancisco3"}, {"AerialImage",
> >   "SanFrancisco4"}, {"AerialImage", "SanFrancisco5"}, {"AerialImage",
> >   "SanFrancisco6"}, {"AerialImage", "SanFrancisco7"}, {"AerialImage",
> >   "SanFrancisco8"}, {"AerialImage", "SanFrancisco9"}, {"AerialImage",
> >   "Shreveport"}, {"AerialImage", "Stockton"}, {"AerialImage",
> >   "Stockton2"}, {"AerialImage", "Stockton3"}, {"AerialImage",
> >   "Stockton4"}, {"AerialImage", "Stockton5"}, {"AerialImage",
> >   "Stockton6"}, {"AerialImage", "WashingtonIR"}, {"AerialImage",
> >   "WoodlandHills"}}
>
> > Any help would be much appreciated and used right away in my daily
> > programming.
>
> > Mac
>
> --
>
> Daniel Huber
>
> Metrohm Ltd.
>
> Oberdorfstr. 68
>
> CH-9100 Herisau
>
> Tel. +41 71 353 8585, Fax +41 71 353 8907
>
> E-Mail:<mailto:d... at metrohm.com>
>
> Internet:<http://www.metrohm.com>



  • Prev by Date: Re: How to use a returned pure function from say, Maximize
  • Next by Date: Re: RenderBadPicture error while running Mathematica 6 on Linux
  • Previous by thread: Re: SetOptions[TableForm] doesn't seem to work?
  • Next by thread: Re: RenderBadPicture error while running Mathematica 6 on Linux