[Date Index]
[Thread Index]
[Author Index]
Re: Basic questions on list manipulation in the "Mathematica Way"
*To*: mathgroup at smc.vnet.net
*Subject*: [mg95649] Re: Basic questions on list manipulation in the "Mathematica Way"
*From*: Albert Retey <awnl at gmx-topmail.de>
*Date*: Fri, 23 Jan 2009 05:10:40 -0500 (EST)
*References*: <gl71s8$c41$1@smc.vnet.net> <gl9n7s$avr$1@smc.vnet.net>
Hi,
> This idea of a function as a database can be used even more:
>
> ClearAll[data];
> data["Thomas Jefferson", "val1"] = 12;
> data["Thomas Jefferson", "val2"] = 34;
> data["Thomas Jefferson", "val3"] = 56;
> data["Thomas Jefferson", "val4"] = 78;
> data["George Washington", "val1"] = 91;
> data["George Washington", "val2"] = 234;
> data["George Washington", "val3"] = 567;
> data["George Washington", "val4"] = 893;
> data["John Kennedy", "val1"] = 21; data["John Kennedy", "val2"] = 33;
> data["John Kennedy", "val3"] = 444; data["John Kennedy", "val4"] =
> 555;
> data["Barack Obama", "val1"] = 377; data["Barack Obama", "val2"] = 12;
> data["Barack Obama", "val3"] = 34;
> data["Barack Obama", "val4"] = 5534;
>
> Modification of a value is now simply a redefinition of the function:
>
> data["Thomas Jefferson", "val1"] = 4444;
>
> This has the disadvantage that it is difficult to collect all values
> of val1.
I agree that this is a useful scheme for storing data in mathematica,
but I don't agree that it is difficult to collect all values of e.g.
val1. Just use DownValues, the following does collect all val1 entries:
Select[DownValues[data], Not[FreeQ[#, "val1"]] &]
(of course there are myriads of other possibilities to collect whatever
you need to once you have the DownValues...)
Another very useful but not very common pattern I often use are
SubValues for this purpose, by defining e.g.
data["Thomas Jefferson"]["val1"] = 12;
data["Thomas Jefferson"]["val2"] = 34;
data["Thomas Jefferson"]["val3"] = 56;
data["Thomas Jefferson"]["val4"] = 78;
data["George Washington"]["val1"] = 91;
data["George Washington"]["val2"] = 234;
data["George Washington"]["val3"] = 567;
data["George Washington"]["val4"] = 893;
data["John Kennedy"]["val1"] = 21;
data["John Kennedy"]["val2"] = 33;
data["John Kennedy"]["val3"] = 444;
data["John Kennedy"]["val4"] = 555;
data["Barack Obama"]["val1"] = 377;
data["Barack Obama"]["val2"] = 12;
data["Barack Obama"]["val3"] = 34;
data["Barack Obama"]["val4"] = 5534;
using this definitions, you can do something like this:
mrpresident = data["Barack Obama"]
mrpresident["val1"]
or, if you like:
mrpresident@"val1"
collecting data can now be done by filtering SubValues[data] accordingly.
> Of course, you could also use external databases, see DatabaseLink/
> tutorial/Overview in the help centre.
and not only just using DatabaseLink on its own is interesting, with the
above scheme(s) and taking advantage of the fact that you can mix
regular definitions with delayed ones you can also easily build
something like e.g. a database read cache which only looks up entries in
the database that it hasn't seen before, with something like:
(* open demo database which comes with mathematica: *)
Needs["DatabaseLink`"]
$demo=OpenSQLConnection["demo"]
(* definition for unseen entries: fetch them from database and put them
in SubValues *)
ClearAll[democache]
democache[namex_String][propx_String]:=Module[{val},
Quiet[
val=SQLSelect[
$demo,"SAMPLETABLE1",
propx,
SQLColumn["Name"]==namex
]
];
If[val=!=$Failed\[And]Length[val]>0,
With[{
name=ToUpperCase[namex],
prop=ToUpperCase[propx]
},democache[name][prop]=val[[1,1]]],
$Failed,$Failed
]
]
(* example usage and checks what is there: *)
democache["Day3"]["Value"]
SubValues[democache]//TableForm
(* some syntactic pleasantries which are for free: *)
day=democache["Day3"];
day["Value"]
day@"Entry"
day@"Name"
(* don't forget to close the SQLConnection when you are done: *)
CloseSQLConnection[$demo]
hth,
albert
Prev by Date:
**Re: Re: Re: Which editor do you use for math**
Next by Date:
**Re: Formatting in text cells ...**
Previous by thread:
**Re: Basic questions on list manipulation in the "Mathematica Way"**
Next by thread:
**Re: Basic questions on list manipulation in the "Mathematica Way"**
| |