Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2009

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

Search the Archive

Re: Conventional way of doing "struct"-like things?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg104601] Re: Conventional way of doing "struct"-like things?
  • From: Uayeb <uayebswinburne at gmail.com>
  • Date: Wed, 4 Nov 2009 01:40:34 -0500 (EST)
  • References: <hcl43p$c1v$1@smc.vnet.net>

Erik,

This is a good question, one I've struggled with a lot as well. Of
particular annoyance is data tables where columns are added and
removed on occasion. It isn't very elligant to have to go back and
edit function definitions or table definitions to correct for these
changes (and it often isn't even apparent if they have changed unless
you have some function which explicitly checks!)

So I have come up with the following alternate solution. Usually, the
data I import from a file has column headings in the first row (which
are just text strings). So I wrote the following function (two parts)
which extracts the column with the appropriate header from the data.

getTabularData[data_List, cols : {_String ..}] :=
 data[[2 ;;, Flatten[Position[data[[1]], #] & /@ cols]]]

getTabularData[data_List, col_String] :=
 data[[2 ;;, First@First[Position[data[[1]], col]]]]

So the data looks something like
id   x     y
1   3.4  4.5
2  34.  7.5
3  5.2  4.

Then I can get the list of y values with
getTabularData[data,"y"]

or the list of {x, y} values with
getTabularData[data,{"x","y"}]

Don't know if this is really appropriate to your situation, but I find
it very helpful, particularly if I later add a column "name" between
"id" and "x"!

Thoughts?

Andy


On Nov 2, 10:03 am, Erik Max Francis <m... at alcyone.com> wrote:
> I find myself (as I'm sure everyone does) dealing with lists of
> structured data, e.g., a list of lists, each of which contains n
> elements with each entry representing a unique field.  What is the most
> conventional way to identify each of these elements when iterating over
> the whole list?  Obviously I can use [[...]]/Part, but I'm wondering if
> there's something considered more elegant.
>
> Take a concrete example where I have a CSV containing ZIP code data,
> with seven fields:  the zip code, latitude, longitude, city, state,
> county, and class.  What's the usual way of addressing this?  I can
> think of two obvious ones; just define symbolic names for the indices:
>
> data = Import[..., "CSV"];
>
> {ZIPCODE, LATITUDE, LONGITUDE, CITY, STATE, COUNTY, CLASS} = Range[7];
>
> ListPlot[{#[[LONGITUDE]], #[[LATITUDE]]} & /@ data,
>   PlotStyle -> PointSize[0.001]]
>
> or define functions (with or without the symbolic index names):
>
> latitude[x_] := x[[2]];
> longitude[x_] := x[[3]];
>
> ListPlot[{longitude[#], latitude[#]} & /@ data,
>   PlotStyle -> PointSize[0.001]]
>
> What's consider more in Mathematica's style, or is there something else
> more commonly used I'm not thinking of?
>
> On that general subject, are there generally-respected Mathematical
> style guides floating around somewhere?  I'm not necessarily looking fo=
r
> rigid codified rules, just for "accepted" (more or less) conventional
> ways of getting things done.  I'm relatively new to Mathematica but by
> no means new to programming and am fluent in many other languages (C,
> C++, Python, etc.), so I'm just looking for ideas about "the way things
> are done" that go well with Mathematica's functional nature but are
> still self-documenting as much as possible.
>
> Thanks.
>
> --
> Erik Max Francis && m... at alcyone.com &&http://www.alcyone.com/max/
>   San Jose, CA, USA && 37 18 N 121 57 W && AIM/Y!M/Skype erikmaxfrancis
>    The chicken was the egg's idea of getting more eggs.
>     -- Samuel Butler



  • Prev by Date: Re: Re: dynamicmodule with f[x_] possible?
  • Next by Date: Re: Re: dynamicmodule with f[x_] possible?
  • Previous by thread: Re: Conventional way of doing "struct"-like things?
  • Next by thread: Re: Conventional way of doing "struct"-like things?