Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-19 04:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found