This sounds like a job for OO. Build two indecies of people objects, one by id and the other by name. Of course, both indecies are hashes where the value is a reference to the correct person object. Then give your object get methods:

result:

{ package Person; sub new { my $proto = shift; my $class = ref($proto) || $proto; my $self = {}; bless ($self, $class); # Add member variables. $self->{Name} = @_ ? shift : die "Person Constructor expec +ted a name"; $self->{Rank} = @_ ? shift : die "Person Constructor expec +ted a rank"; $self->{SerialNumber} = @_ ? shift : die "Person Constructor expec +ted a serial number"; return $self; } sub Name { my $self = shift; return $self->{Name}; } sub SerialNum { my $self = shift; return $self->{SerialNumber}; } # And whatever other methods you want. } my $dad = Person->new( 'dad', 'top dog', 1 ); my %ByName; while( ReadPeopleDataFromSomewhere() ) { my $person = Person->new( $name, $rank, $serial ); # Error check for repeat names? # I didn't, but that might not be a bad idea. $ByName{$person->Name()} = $person; # Likewise, one hopes that serial numbers are unique. $BySerial{$person->SerialNum()} = $person; }

The above sample code is untested but gives you the idea. You should definately read perltoot.


In reply to Re: Complex Data Structures? by Adam
in thread Complex Data Structures? by Anonymous Monk

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.