you're supposed to use hash references into arrays?

The data structure you're probably looking for is a hash, where each value is a reference to an individual array - a "hash of arrays". The links that Happy-the-monk provided above should explain.

But I don't see how(in example) typing in the "2" would pull up both BBB AND EEE

"Access and Printing of a HASH OF ARRAYS" in perldsc shows you some examples of how to access the data structure. It'd probably also be helpful to you if you learn about how to handle references in general, e.g. perlreftut.

To get you started, here's one way to build such a structure from the input you showed. One important concept here is autovivification, which is what causes you to be able to write @{$data{$k}} and have a reference to a new array spring into existence ("vivify") even if $data{$k} was previously undefined.

use warnings; use strict; my %data; while (<DATA>) { chomp; next unless /\S/; my ($k,$v) = /^\s*(\S+)\s+(.+?)\s*$/; push @{$data{$k}}, $v; } use Data::Dumper; print Dumper(\%data); __DATA__ 1 AAA 2 BBB 3 CCC 4 DDD 2 EEE

Output:

$VAR1 = { '3' => [ 'CCC' ], '1' => [ 'AAA' ], '4' => [ 'DDD' ], '2' => [ 'BBB', 'EEE' ] };

In reply to Re: duplicate keys on hash by Anonymous Monk
in thread duplicate keys on hash 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.