6.40 Making struct/record and array of structs

There is no build-in struct or record in Mathematica. But this is what I do. Since in M a matrix can include non-numeric data, I use a list for a record, and then use a matrix to make an array of records (or array of structs). I just need to make a constant to indicate the field name in the record, to make it easier to reference. Here is an example

id = 1;  (*first field name*) 
pop = 2; (*second field name*) 
name = 3; (*third field name*) 
 
(*now build the array of record, each row is a record*) 
m = {{1, 3000, "London"}, 
     {1, 6000, "New York"}, 
     {3, 7300, "Moscow"}}; 
 
(*now can iterate over the records*) 
 
Do[ 
 Print@m[[i, id]]; 
 m[[i, pop]] += 1, 
 {i, Length[m]} 
]
 

Ok, not very fancy, but easy to setup and data works with all M other functions, since it is just a list of lists.

Some more links on the subject

  1. struct-data-type-in-mathematica
  2. setting-up-a-struct-in-mathematica-safely
  3. using-a-struct-inside-manipulate-to-help-manage-control-variables-how-to-initia
  4. struct-equivalent-in-mathematica