Re: data structures
- To: mathgroup at smc.vnet.net
- Subject: [mg22697] Re: data structures
- From: "Allan Hayes" <hay at haystack.demon.co.uk>
- Date: Wed, 22 Mar 2000 00:28:04 -0500 (EST)
- References: <8av7mf$jab@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Maarten, I give below a quick and simple response, taking up the pattern of your example. This clearly has lots of problems. But Roman Maeder, The Mathematica Progammer, has a chapter on Databases. person[i_, x : street | city] := person[i, address, x]; person[i_, address] := person[i, #] & /@ {street, city}; person[1, name] = "Jeff"; person[1, length] = 180; person[1, address, street] = "Church Road"; person[1, address, city] = "London"; person[1, favouriteColours[1]] = "green"; person[1.favouriteColours[2]] = "purple"; person[2, name] = "Jane"; person[2, length] = 175; person[2, address, street] = "Farm Road"; person[2, address, city] = "York"; person[2, favouriteColours[1]] = "pink"; person[2, favouriteColours[2]] = "green"; Do[Print[person[i, street]], {i, 2}] Print[person[1, address] // TableForm] Sum[person[i, length], {i, 2}] -- Allan --------------------- Allan Hayes Mathematica Training and Consulting Leicester UK www.haystack.demon.co.uk hay at haystack.demon.co.uk Voice: +44 (0)116 271 4198 Fax: +44 (0)870 164 0565 <Maarten.vanderBurgt at icos.be> wrote in message news:8av7mf$jab at smc.vnet.net... > Hi, > > Is there an elegant way of setting up data structures like you can do in > languages like C? > Something like the following example (what follows is a mixture between > mathematica code and C) explains what I am looking for: > > Table[ person[i], {i,1,10}] > > person[1].name = "Jeff" > person[1].length = 180 > person[1].address.street = "Church Road" > person[1].address.city = "London" > person[1].favourite_colours[1]= "green" > person[1].favourite_colours[2]= "pruple" > > person[2].name = "Jane" > person[2].length = 175 > person[2].address.street = "Farm Road" > ... > > person[10].... > > Then you can do things like: > > Do[Print[ person[i].street ], {i,1,10}], > > or > > totallength = Apply[Plus, Table[ person[i].length, {i,1,10}]]. > > > > I know you could do something similar with nested lists. > But I do not want to remember that index 1 corresponds to 'name', index 2 > corresponds to 'length' and that index 3,2 is 'city' and so on. > > > Any suggestions are welcome. > Thanks > > Maarten van der Burgt > Leuven, Belgium > > > > > > >