Re: Basic questions on list manipulation in the "Mathematica Way"
- To: mathgroup at smc.vnet.net
- Subject: [mg95577] Re: Basic questions on list manipulation in the "Mathematica Way"
- From: dh <dh at metrohm.com>
- Date: Thu, 22 Jan 2009 06:58:25 -0500 (EST)
- References: <gl71s8$c41$1@smc.vnet.net>
Hi, I would store the data in a function. Further, I would not store attribute names more than once. Here is an example: person[John]={1,2,3} this declares a person "John" with attribute values: 1,2,3 To pick a certain attribute: person[name_,attr1]:= person[name][[1]]; person[name_,attr2]:= person[name][[2]]; person[name_,attr3]:= person[name][[2]]; This scheme can be extended. hope this helps, Daniel dangerrity at gmail.com wrote: > Hello, > > I have some basic questions about using lists in Mathematica. > Specifically I have a table that serves as sort of a database with the > following structure: > > ct = { > { sym1 -> {val1->20, val2->300, val3->1000, ... }, > { sym2 -> {val1->50, val2->500, val3->20000,...}, > ... > } > > sym1, sym2, ... are people and val1, val2 ... represent attributes of > each person. > > Now I'm trying to go in and modify values. I can do it, but I think > not well and I think with a "programming language" mindset instead of > a "Mathematica mindset." > > Here is my specific question. In order to change a specific value in > the list above (valxxx) for a given individual (symxxx), I created > this function: > > changeVal[ who_, class_, amnt_ ] := ( > ct[[ Position[ ct, who ][[ 1, 1 ]], 2 ]] = > ReplacePart[ ct[[ Position[ ct, who ][[ 1, 1 ]], 2 ]], > Position[ ct[[ Position[ ct, who ][[ 1, 1 ]] ]], class ][[ 1, > 2 ]] -> (class -> amnt) ] > ); > > Now I know there is a better way than that using list manipulation and > patterns. Can some of you experienced pros help me out? I call this > "write only" code because I don't know that I could explain it once > it's written. > > Perhaps a more fundamental question: is this the right way to store > data in lists? Or would it be better to just have the values and > reference them by index number? > > Thanks for your patience with a simple question. > >