MathGroup Archive 2003

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

Search the Archive

Re: structs/records in mathematica

  • To: mathgroup at smc.vnet.net
  • Subject: [mg41775] Re: [mg41767] structs/records in mathematica
  • From: jmt <jmt at dxdydz.net>
  • Date: Thu, 5 Jun 2003 07:31:21 -0400 (EDT)
  • Organization: dxdydz
  • References: <200306041234.IAA17798@smc.vnet.net>
  • Reply-to: jmt at dxdydz.net
  • Sender: owner-wri-mathgroup at wolfram.com

A suggestion :

1 - Tag your record/structure, with whatever name your like, for instance 
DataSlice ;

2 - Define rules attached to the name (upvalues), for instance :
DataSlice/:expr_/.DataSlice[rules___]:=expr/.{rules}

You can then access your fields, for instance :

myDataSlice=DataSlice[id->"whatever you put 
here",value=1/4,label="identification"]

id/.myDataSlice should return "whatever you put here"
and so on.

You can enhance this process with :
Format[DataSlice[x___]:=label/.{x}
This construct limits/selects the amount of information displayed when calling 
an "object" like DataSlice.

On Wednesday 04 June 2003 14:34, Sidney Cadot wrote:
> Hi All,
>
> Currently I am involved in a project to read satellite data files into
> languages such as IDL and others. We're also considering implementing
> a Mathematica interface.
>
> For this application, we need the ability to represent classical
> records/structs (as found in imperative languages such as C/Pascal),
> having named fields with an associated value.
>
> Since such a type is not directly supported in Mathematica, we'd like
> to know if there is a 'standard' way of handling this type of
> situation.
>
> We're currently thinking of using a list of rules, e.g.
>
> data = {
>   id->"this_or_that",
>   value->17.3,
>   compound->{id->"compound_id", value->18.4}
> }
>
> but this makes selecting a value a bit awkward:
>
> value/.(compound/.data)
>
> since this is syntactically the reverse of what one would expect in a
> language like C/Pascal (data.compound.value), and the /. operator is
> left-associative (therefore requiring parentheses in case of nested
> structs).
>
> Is there perhaps a better way/standard idiom for handling situations
> like this?
>
> Best regards,
>   Sidney Cadot
>   Science and Technology Corp., The Netherlands


  • Prev by Date: Re: Re: Big problem in solving radicals.
  • Next by Date: Re: Big problem in solving radicals.
  • Previous by thread: structs/records in mathematica
  • Next by thread: Re: structs/records in mathematica