I am trying to create a hash of arrays from a file. I've put the file into an array and then I'm going through the array element by element. For each element I assign a value to a variable that I would like to add as a new key in a hash. I also create an array from the element that I'd like to use as the value for the hash. So for each element in the array, I want to add a new key and value pair to the hash.

Here's what I am using:
open (BINFILE, $binaryfile) or die "Cannot open $binaryfile\n"; my @lines; while (<BINFILE>) { push (@lines, $_); } my %bin_file_data = (); for (@lines) { my @split = split (',', $_); my $sampname = $split[0]; my $binnumbers = $split[1]; my @bindata = split ('\t', $binnumbers); #I want $sampname as the key associated with the array @bindata $bin_file_data{$sampname} = @bindata; } print Dumper (\%bin_file_data); close BINFILE;

My Data Dump is giving me something like this (simplified):

$VAR1 = { 'sampname1' => 1032, 'sampname2' => 1032, };

1032 is the number of elements in @bindata. Am I just using DataDumper incorrectly or am I making an error in creating my hash?


In reply to Creating Hash of Arrays by GeneGeek

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.