GeneGeek has asked for the wisdom of the Perl Monks concerning the following question:
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?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Creating Hash of Arrays
by kennethk (Abbot) on Sep 09, 2015 at 22:37 UTC | |
by GeneGeek (Initiate) on Sep 09, 2015 at 22:44 UTC | |
by GotToBTru (Prior) on Sep 10, 2015 at 19:57 UTC | |
|
Re: Creating Hash of Arrays
by stevieb (Canon) on Sep 09, 2015 at 22:41 UTC |