1) Try to write a program that does the following. Create a file with these lines in it:

one hello two world three goodbye two apple one banana

Now, collect all the words in the second column that have the same word in the first column. You want your program to display this(in any order):

one: hello, banana two: world, apple three: goodbye

Spend an hour trying to write that program.

2) Next, read perlreftut. There is no point in discussing references unless you know the basics.

3) A line such as this:

push @{$hash_name{$key}}, $string;

is magical. This part:

$hash_name{$key}

tells perl to retrieve the value from the hash that corresponds to the key. The @{ } around $hash_name{$key} tells perl to convert the value to an array--so you know the retrieved value has to be an array reference. Then push() adds $string to the array.

Now, here is the magic part: if the key does not exist in the hash, normally $hash_name{$key} would return undef, and then you would be using @{ } to convert undef to an array, which doesn't work. However, perl magically creates a reference to an empty array for you, and subsequently @{ } converts the reference to an empty array. Then push adds $string to the empty array.

In effect, the line:

push @{$hash_name{$key}}, $string;

tells perl to add $string to the array corresponding to $hash_name{$key}, but if the $key does not exist in the hash, create the key with a corresponding empty array, and add $string to the empty array.

Here's the program:

data1.txt:

one hello two world three goodbye two apple one banana

========

use strict; use warnings; use 5.010; open my $INFILE, '<', 'data1.txt' or die "Couldn't open file: $!"; my %hash; while (<$INFILE>) { chomp; my ($first_col, $second_col) = split; push @{$hash{$first_col}}, $second_col; } while ( my($first_col, $aref) = each %hash ) { say "$first_col: ", join(', ', @$aref); } close $INFILE; --output:-- three: goodbye one: hello, banana two: world, apple

In reply to Re^3: Help wanted .. cant get my head around array of hash by 7stud
in thread Help wanted .. cant get my head around array of hash by theantler

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.