MathGroup Archive 2000

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

Search the Archive

RE: data structures

  • To: mathgroup at smc.vnet.net
  • Subject: [mg22703] RE: [mg22662] data structures
  • From: "Ersek, Ted R" <ErsekTR at navair.navy.mil>
  • Date: Wed, 22 Mar 2000 00:28:09 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

Maarten van der Burgt Leuven wrote:
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. 

-----------------------------------------------
My approach is to give person multiple arguments as in:

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"
 ... 
---------------------------------
However name, address, street, length, etc. may have assigned values, so you
may want to use strings instead. In that case you would have:
person[1, "address", "street"] = "Church Road" 
Another approach is to use nested heads as in:
person[1][address][street] = "Church Road" 
or
person[1]["address"]["street"] = "Church Road" 
I don't see that there is any advantage in telling Mathematica you have 10
persons.

--------------------
Regards,
Ted Ersek

See Mathematica tips, tricks at
http://www.verbeia.com/mathematica/tips/Tricks.html



  • Prev by Date: Re: Re: [Q] Differential equation?
  • Next by Date: how to find roots of Bessel zero's
  • Previous by thread: Re: data structures
  • Next by thread: A Version 4 Update