update: After writing the following, I realized you don't want multiple keys, you want one key incorporating multiple values. markkawika and bichonfrise74 have already shown you how to combine the values to create a composite key. You may incorporate that idea with the hash of lists below to deal with duplicate keys.

You can create as many hashes as you want, so multiple keys to the same data is not a problem. You can also create a hash of arrays, where each hash value is a reference to an array, allowing you to store multiple values for each key. This might address your issue with collisions. Whatever combination of first, middle and last name you use for keys, you will have the possibility of collisions, so a hash of arrays is probably the way to go. The following demonstrates one way of doing it.

use strict; use warnings; my ( %individuals_by_first_name, %individuals_by_middle_name); foreach my $line (<DATA>) { chomp($line); my ($first, $middle, $last, $age, $sex, $location) = split(/,/, $l +ine); my $record = [ $first, $middle, $last, $age, $sex, $location ]; push(@{$individuals_by_first_name{$first}}, $record); push(@{$individuals_by_middle_name{$middle}}, $record); } foreach my $first (sort keys %individuals_by_first_name) { foreach my $individual (@{$individuals_by_first_name{$first}}) { my ($first, $middle, $last, $age, $sex, $location) = @$individ +ual; print "$first: $middle, $last, $age, $sex, $location\n"; } } __DATA__ first1,middle1,last1,27,M,here first2,middle2,last2,56,M,there first3,middle3,last3,30,F,everywhere first1,middle4,last4,7,F,home first4,middle2,last3,22,M,away

which produces

first1: middle1, last1, 27, M, here first1: middle4, last4, 7, F, home first2: middle2, last2, 56, M, there first3: middle3, last3, 30, F, everywhere first4: middle2, last3, 22, M, away

or, by adding one more layer of hash, creating a hash of hashes of arrays, you can add flexibility to add additional keys without adding variables and merge the two hashes into one.

use strict; use warnings; my %individuals; foreach my $line (<DATA>) { chomp($line); my ($first, $middle, $last, $age, $sex, $location) = split(/,/, $l +ine); my $record = [ $first, $middle, $last, $age, $sex, $location ]; push(@{$individuals{first}{$first}}, $record); push(@{$individuals{middle}{$middle}}, $record); } foreach my $first (sort keys %{$individuals{first}} ) { foreach my $individual (@{$individuals{first}{$first}}) { my ($first, $middle, $last, $age, $sex, $location) = @$individ +ual; print "$first: $middle, $last, $age, $sex, $location\n"; } } __DATA__ first1,middle1,last1,27,M,here first2,middle2,last2,56,M,there first3,middle3,last3,30,F,everywhere first1,middle4,last4,7,F,home first4,middle2,last3,22,M,away

This produces the same output as the first example. You can read more about references and data structures in perlref, perldsc, perllol and References.


In reply to Re: question on Hashes by ig
in thread question on Hashes by kayj

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.