Hello oh wise monks,

I am trying to construct a Hash of an Array and frankly I am making a Hash of it. This is what I am trying to achieve

  1. I am creating the hash with the keys only and no values. This is done by reading in a file and splitting out values based on the same position. This also includes removing tabs and white space, creating an array to be used later and using a temp hash to weed out any duplicates. The file I am reading in looks like this (there are multiple lines to the file):
    # Comments - To be ignored field 1 ;field 2 ;field 3 ;field 4 ;field 5; field 6;
    Fields 5 onwards are not needed for this process. Field 4 is what I want to use for the key, but is not unique. Field 1 is then going to be used to populate the array in the hash and is unique for each line. The first part of the code, to create the empty hash is done like this:
    while (<FILE>) { chomp; if ($_ !~ m/#/) { s/\t//g; s/\s//g; ($f1,$f2,$f3,$f4,$f5) = split (/;/, $_,5); push @file,$_; unless ($seen{$f4}) { $seen{$f4}=1; $mailhouse{$f4} = ""; } } }
    This seems to work
  2. What I am doing next, is to then search against @file the array that I have made, using the hash keys to match against field 4. If field 4 is a match, I then want to push field 1 into the array for the hash with that key:
    for $mh ( keys %mailhouse ) { foreach (@file) { ($g1,$g2,$g3,$g4,$g5) = split (/;/, $_,5); if ($mh eq $g4) { push @{ $mailhouse{$mh} }, $g1; } } }
    This is the part that does not work.

What seems to happen is once the field 1 is pushed into the array, it seems to populate for all the keys of the hash.

Can anyone point me in the right direction? Can anyone suggest an alternative?

Cheers, the hoff


In reply to A Hash that is giving me the ####s by hoffy

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.