G'day Tmms,

Welcome to the Monastery.

"I am fairly new at perl and I am struggling with references."

Take a look at "perlreftut - Perl references intro" for general information on references; it contains links to more detailed information - follow as needed.

For general information on data structures, see "perldsc - Perl data structures intro"; again, this has links to more detailed documentation.

The type of data structure you're attempting to create is called a Hash of Arrays (typically abbreviated to HoA). If you look at "Generation of a HASH OF ARRAYS" (in perldsc), the last line of the example code is pretty much what you're looking for.

So, working from just your example data, your code would look something like this:

#!/usr/bin/env perl use strict; use warnings; use Data::Dump; my %hash; while (<DATA>) { chomp; my ($value, $key) = split /\t/; push @{$hash{$key}}, $value; } dd \%hash; __DATA__ Obi Wan Jedi Yoda Jedi Count Dooku Sith

Output:

{ Jedi => ["Obi Wan", "Yoda"], Sith => ["Count Dooku"] }

While that works fine with your (ideal) example data, real world data throws up all sorts of nasty challenges. Whenever you're working with comma- (tab-, pipe-, whatever-) separated values, I recommend you use the Text::CSV module. If you also have Text::CSV_XS installed, it will run faster. This module has addressed these "nasty challenges": this is not a wheel you need to reinvent. In this instance, the while loop only needs one statement.

#!/usr/bin/env perl use strict; use warnings; use Text::CSV; use Data::Dump; my %hash; my $csv = Text::CSV::->new({sep_char => "\t"}); while (my $row = $csv->getline(\*DATA)) { push @{$hash{$row->[1]}}, $row->[0]; } dd \%hash; __DATA__ Obi Wan Jedi Yoda Jedi Count Dooku Sith

Output (exactly the same as before):

{ Jedi => ["Obi Wan", "Yoda"], Sith => ["Count Dooku"] }

See also: Data::Dump (which provides the dd function) or use Data::Dumper (which is a core module).

— Ken


In reply to Re: Making a hash of arrays using references by kcott
in thread Making a hash of arrays using references by Tmms

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.