It depends on how you want to access the data. A HoA keyed on the first column won't work since the values in that column are not unique. A HoH would be ok if the second key was unique within each first key (the example data has a duplicate email address but it is for two different people). Without that guarantee you could go with a HoAoA (or a HoAoH) if you wanted to look data up based on the values in the first column. If there are no lookup requirements you could just use an AoA.

BTW, you only end up with one item in the hash because you clear it out when you assign to %hash every time through the loop.

my @AoA; my %HoAoA; my %HoAoH; while( my $line = <DATA> ){ chomp $line; my @fields = split( /\s+\s+/, $line ); # Add an array ref (containing the data in @fields) onto the @AoA push( @AoA, [ @fields ] ); # Add an array ref (containing the last 2 elements of @fields) # onto the %HoAoA. The key in the %HoAoA is $fields[0], and # the value is an array ref. '@fields[ 1, 2 ]' is an array # slice and is shorthand for ( $fields[1], $fields[2] ) push( @{ $HoAoA{$fields[0]} }, [ @fields[ 1, 2 ] ] ); # Add a hash ref onto the %HoAoH. The key in the %HoAoH is # $fields[0], and the value is an array ref. The array # contains one or more hash refs. Each hash ref # contains the 'email' and 'result' keys. push( @{ $HoAoH{$fields[0]} }, { email => $fields[1], result => $fields[2] } ); }

Output:

AoA

$VAR1 = [ [ 'susan', 'anus@delta.co.th', 'cannot find your hostname' ], [ 'jennifer', 'r_curran@ntnu.no', 'cannot find your hostname' ], [ 'jennifer', 'jones@bol.net.in', 'cannot find your hostname' ], [ 'jennifer', 'millionairemaker@freshcornam.com', 'cannot find your hostname' ], [ 'tim', 'millionairemaker@freshcornam.com', 'cannot find your hostname' ], [ 'jennifer', 'cmayer@kareltek.fi', 'cannot find your hostname' ], [ 'jack', 'ictiplo@tiplo.com.tw', 'cannot find your hostname' ], [ 'russ', 'orders@koss.com', 'Host not found' ], [ 'clara', 'Sandra@camrpc.com', 'Host not found' ], [ 'jennifer', 'Warranty@onlogixx.com', 'cannot find your hostname' ], [ 'employment', 'elitemate@rllformat.com', 'cannot find your hostname' ], [ 'jack', 'JHuffman@pulsetech.net', 'cannot find your hostname' ], [ 'clara', 'Sandra@camrpc.com', 'Host not found' ], [ 'susan', 'linda@kepro.com.tw', 'cannot find your hostname' ], [ 'jobs', 'kwillis@cors.com', 'Host not found' ], [ 'employment', 'LifeShopDirect@freshcornam.com', 'Host not found' ] ];

HoAoA

$VAR1 = { 'jack' => [ [ 'ictiplo@tiplo.com.tw', 'cannot find your hostname' ], [ 'JHuffman@pulsetech.net', 'cannot find your hostname' ] ], 'jobs' => [ [ 'kwillis@cors.com', 'Host not found' ] ], 'jennifer' => [ [ 'r_curran@ntnu.no', 'cannot find your hostname' ], [ 'jones@bol.net.in', 'cannot find your hostname' ], [ 'millionairemaker@freshcornam.com', 'cannot find your hostname' ], [ 'cmayer@kareltek.fi', 'cannot find your hostname' ], [ 'Warranty@onlogixx.com', 'cannot find your hostname' ] ], 'tim' => [ [ 'millionairemaker@freshcornam.com', 'cannot find your hostname' ] ], 'susan' => [ [ 'anus@delta.co.th', 'cannot find your hostname' ], [ 'linda@kepro.com.tw', 'cannot find your hostname' ] ], 'russ' => [ [ 'orders@koss.com', 'Host not found' ] ], 'employment' => [ [ 'elitemate@rllformat.com', 'cannot find your hostname' ], [ 'LifeShopDirect@freshcornam.com', 'Host not found' ] ], 'clara' => [ [ 'Sandra@camrpc.com', 'Host not found' ], [ 'Sandra@camrpc.com', 'Host not found' ] ] };

HoAoH

$VAR1 = { 'jack' => [ { 'result' => 'cannot find your hostname', 'email' => 'ictiplo@tiplo.com.tw' }, { 'result' => 'cannot find your hostname', 'email' => 'JHuffman@pulsetech.net' } ], 'jobs' => [ { 'result' => 'Host not found', 'email' => 'kwillis@cors.com' } ], 'jennifer' => [ { 'result' => 'cannot find your hostname', 'email' => 'r_curran@ntnu.no' }, { 'result' => 'cannot find your hostname', 'email' => 'jones@bol.net.in' }, { 'result' => 'cannot find your hostname', 'email' => 'millionairemaker@freshcornam.c +om' }, { 'result' => 'cannot find your hostname', 'email' => 'cmayer@kareltek.fi' }, { 'result' => 'cannot find your hostname', 'email' => 'Warranty@onlogixx.com' } ], 'tim' => [ { 'result' => 'cannot find your hostname', 'email' => 'millionairemaker@freshcornam.com' } ], 'susan' => [ { 'result' => 'cannot find your hostname', 'email' => 'anus@delta.co.th' }, { 'result' => 'cannot find your hostname', 'email' => 'linda@kepro.com.tw' } ], 'russ' => [ { 'result' => 'Host not found', 'email' => 'orders@koss.com' } ], 'employment' => [ { 'result' => 'cannot find your hostname', 'email' => 'elitemate@rllformat.com' }, { 'result' => 'Host not found', 'email' => 'LifeShopDirect@freshcornam.c +om' } ], 'clara' => [ { 'result' => 'Host not found', 'email' => 'Sandra@camrpc.com' }, { 'result' => 'Host not found', 'email' => 'Sandra@camrpc.com' } ] };

See perldsc and perllol for more info on navigating these structures.

HTH


In reply to Re: HoH question by bobf
in thread HoH question by cajun

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.