in reply to Complex Data Structures?

Well, I'm just taking a guess as to what you want from the data you have supplied, but a hash of hashes might work
my %people = ( 7 => { name => 'Bob Jones', address => '555 Anywhere Street', city => 'Allentown', state => 'Pennsylvania' }, 3 => { name => 'Pete Hollister', address => '222 Northview Drive', city => 'Shoreham', state => 'unknown' } ); my $idnumber = 3; print $people{ $idnumber }{ name };
The above code prints "Pete Hollister". Here's the same thing with a hash of arrays:
my %people = ( 7 => [ 'Bob Jones', '555 Anywhere Street', 'Allentown', + 'Pennsylvania' ], 3 => [ 'Pete Hollister', '222 Northview Drive', 'Shoreh +am', 'unknown' ] ); my $idnumber = 3; print $people{ $idnumber }[0];
Or, to be complete obnoxious (and I don't recommend this), we could go with an array of pseudo-hashes which would allow you to use this as a hash of hashes or a hash of arrays as you like:
my $phashref = { name => 1, address => 2, city => 3, state => 4 }; my %people = ( 7 =>[$phashref, 'Bob Jones', '555 Anywhere Street', 'Al +lentown', 'Pennsylvania' ], 3 =>[$phashref, 'Pete Hollister', '222 Northview Drive' +, 'Shoreham', 'unknown' ] ); my $idnumber = 3; print $people{ $idnumber }->{ name } . "\n"; print $people{ $idnumber }->[1];
The above resulted from my being a bit bored at work and having some free time. I was just playing around and I strongly advise against this approach. Aside from the difficulties with maintenance, merlyn mentioned that pseudo-hashes are probably not going to be in Perl 6.

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Replies are listed 'Best First'.
Re: (Ovid) Re: Complex Data Structures?
by myocom (Deacon) on Dec 21, 2000 at 03:44 UTC

    Add my vote for the hash of hashes bit. It makes your code easier to maintain than a hash of lists. Plus, if you end up adding a field (zip?), you'll probably find it more intuitive to use $people{$idnumber}{zip} than $people{$idnumber}[4].

    See also perldsc.

Re: (Ovid) Re: Complex Data Structures?
by Anonymous Monk on Dec 21, 2000 at 03:52 UTC
    Lets say I want to know the city and all I have is the person's name. How would I find this information without a loop, going through each key of the hash until the persons named matched? Thanks a lot for your quick responses!
      As has been suggested, you may want to consider a database. However, if all you have is a person's name, how do you ensure that the names are unique? If you have two names set to "Twinkletoes", you're going to have a problem.

      Assuming that you have unique names, why not drop the id number? However, I wouldn't drop the it - a person may get married, change their name, have their real identity exposed, etc. But if you're not storing the names for long, it may not be relevant.

      An id number is used to provide a non-identifying means of getting to the data that you want. One way to do this is to map the cities to an array (we assume you might have more than one city because you may have duplicate names):

      my %people = ( 7 => { name => 'Bob Jones', address => '555 Anywhere Street', city => 'Allentown', state => 'Pennsylvania' }, 3 => { name => 'Pete Hollister', address => '222 Northview Drive', city => 'Shoreham', state => 'unknown' }, 9 => { name => 'Pete Hollister', address => '222 Northview Drive', city => 'New York', state => 'unknown' } ); my $name = 'Pete Hollister'; my @cities = map { $people{ $_ }{ name } eq $name ? $people{ $_ }{ cit +y } : () } keys %people; $,=', '; # This sets the array separator to a comma and space, allowin +g # easy printing of arrays print @cities;
      That should print "New York, Shoreham".

      However, if names are guaranteed to be unique, use them as the hash key and just print $people{ $name }{ city }. This is really the only way you're going to avoid a loop unless you have the unique id number up front.

      You're facing the problem that as your data grows more complex, your code usually must grow more complex with it. Take the information above and stuff it away into a subroutine, or better still, take Adam's suggestion and go with an object-oriented module. I'm guessing that you may not be familiar with the latter due to the nature of your question, but it's easier than you might suspect once you get used to the syntax and the new way of thinking.

      Cheers,
      Ovid

      Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.