MathGroup Archive 2009

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

Search the Archive

Conventional way of doing "struct"-like things?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg104490] Conventional way of doing "struct"-like things?
  • From: Erik Max Francis <max at alcyone.com>
  • Date: Sun, 1 Nov 2009 17:55:41 -0500 (EST)

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 for 
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 && max 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: How do I get Mathematica to Simplify this to 1?
  • Next by Date: Re: Problem with Screen Environment
  • Previous by thread: RE: Re: Plot using DSolve HELP
  • Next by thread: Re: Conventional way of doing "struct"-like things?