OK

I use Moose

First I made the class holding the whole structure. I called it Seismic.

use strict; package Seismic; package Seismic::Station; use Moose; has 'Easting' => (isa => 'Int', is => 'rw', required => 1); has 'Northing' => (isa => 'Int', is => 'rw', required => 1); has 'Elevation' => (isa => 'Int', is => 'rw', required => 1); has 'Id' => (isa => 'Str', is => 'rw', required => 1); package Seismic::Line; use Moose; use MooseX::AttributeHelpers; has 'Length' => (isa => 'Int', is => 'rw'); has 'GroupInterval' => (isa => 'Int', is => 'rw'); has 'Id' => (isa => 'Str', is => 'rw', req +uired => 1); has 'Stations' => ( metaclass => 'Collection::Hash', is => 'rw', isa => 'HashRef[Seismic::Station]', default => sub { {} }, provides => { exists => 'station_exists', keys => 'station_ids', get => 'get_station', set => 'add_station', count => 'count_stations', delete => 'delete_station', clear => 'delete_all_stations', }, ); package Seismic::Grid; use Moose; has 'Lines' => ( metaclass => 'Collection::Hash', is => 'rw', isa => 'HashRef[Seismic::Line]', default => sub { {} }, provides => { exists => 'line_exists', keys => 'line_ids', get => 'get_line', set => 'add_line', count => 'count_lines', delete => 'delete_line', clear => 'delete_all_lines', }, ); 1;

And this is the program:

use strict; use Seismic; my $grid = Seismic::Grid->new(); for my $line (1 .. 1000) { my $lineobject = Seismic::Line->new( { Id => $line, Length => int(1000 * rand(90)), GroupInterval => int( 100 * rand(90)), } ); $grid->add_line($lineobject->Id, $lineobject); for my $station (1 .. 10) { my $stationobject = Seismic::Station->new( { Id => $line . '_' . $station, Northing => int(1e6 * rand(90)), Easting => int(1e6 * rand(90)), Elevation => int( 10 * rand(90)), } ); $lineobject->add_station($stationobject->Id, $stationobject); } } print "line station position elev.\n"; for my$line_id ($grid->line_ids) { my $line = $grid->get_line($line_id); for my $station_id ($line->station_ids) { my $station = $line->get_station($station_id); printf "%08d %-8s %08dN%08dW %4d\n", $line->Id, $station->Id, $station->Northing, $station->Easting, $station->Elevation; } }
Everything is stored in one $grid object which consists of $line objects which hold the $station objects.

Direct access to a station and its attributes is easy:

$grid->get_line(257)->get_station('257_5')->Elevation;
Of course, behind the scenes, it is all hashes and arrays and hashes of hashes and ... . And of course it is much slower than programming these data structures directly, but then you don't get these nice accessors, mutators, type-checking, default values, ...

PS: I did not implement all the fields in all the objects, just enough --I hope-- to show it works.

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James


In reply to Re^5: Data Structures by CountZero
in thread Data Structures by YYCseismic

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.