Re: Datafunctions - multiple keys
- To: mathgroup at smc.vnet.net
- Subject: [mg89228] Re: Datafunctions - multiple keys
- From: dh <dh at metrohm.ch>
- Date: Fri, 30 May 2008 02:56:28 -0400 (EDT)
- References: <g1m8gv$j5$1@smc.vnet.net>
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:dh at metrohm.com> Internet:<http://www.metrohm.com>