MathGroup Archive 2000

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

Search the Archive

Re: data structures

  • To: mathgroup at smc.vnet.net
  • Subject: [mg22685] Re: [mg22662] data structures
  • From: Jacqueline Zizi <jazi at club-internet.fr>
  • Date: Wed, 22 Mar 2000 00:27:55 -0500 (EST)
  • References: <200003180627.BAA19667@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Answer
=====

No problem, in Mathematica it is more simple, if you are coherent about
notations:

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";


Do[Print[person[i][address][street]], {i, 1, 4}] give:

Church Road
Farm Road
person[3][address][street]
person[4][address][street]

totallength=Apply[Plus,Table[person[i][length],{i,1,7}]] give:

355+person[3][length]+person[4][length]+person[5][length]+person[6][length]+person[7][length]

Notice that you don't need to write at the beginning: Table[ person[i],
{i,1,10}] or whatever and that you can work with unknown data .

This is a solution fitting as close as possible to your question. There is
other solutions, some of them more simpler or more useful, depending on what
your are going to do with the data.


Question
======


> 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



  • Prev by Date: Re: Monte Carlo
  • Next by Date: Re: FindMinimum
  • Previous by thread: data structures
  • Next by thread: Re: data structures