http://www.perlmonks.org?node_id=937177

Jeri has asked for the wisdom of the Perl Monks concerning the following question:

Hi! I'm trying to create a hash of hashes, but I keep getting the following error.

Odd number of elements in anonymous hash at HoH.pl line 69, <$TABLE> l +ine 3. Odd number of elements in anonymous hash at HoH.pl line 69, <$TABLE> l +ine 3. Odd number of elements in anonymous hash at HoH.pl line 69, <$TABLE> l +ine 3. Odd number of elements in anonymous hash at HoH.pl line 69, <$TABLE> l +ine 4. Odd number of elements in anonymous hash at HoH.pl line 69, <$TABLE> l +ine 4. Odd number of elements in anonymous hash at HoH.pl line 69, <$TABLE> l +ine 4. Odd number of elements in anonymous hash at HoH.pl line 69, <$TABLE> l +ine 4. Odd number of elements in anonymous hash at HoH.pl line 69, <$TABLE> l +ine 4. Odd number of elements in anonymous hash at HoH.pl line 69, <$TABLE> l +ine 4. Odd number of elements in anonymous hash at HoH.pl line 69, <$TABLE> l +ine 4. Odd number of elements in anonymous hash at HoH.pl line 69, <$TABLE> l +ine 4. Odd number of elements in anonymous hash at HoH.pl line 69, <$TABLE> l +ine 4. Odd number of elements in anonymous hash at HoH.pl line 69, <$TABLE> l +ine 4. Odd number of elements in anonymous hash at HoH.pl line 69, <$TABLE> l +ine 4. Odd number of elements in anonymous hash at HoH.pl line 69, <$TABLE> l +ine 4. Odd number of elements in anonymous hash at HoH.pl line 69, <$TABLE> l +ine 4. Odd number of elements in anonymous hash at HoH.pl line 69, <$TABLE> l +ine 5. Odd number of elements in anonymous hash at HoH.pl line 69, <$TABLE> l +ine 5. Odd number of elements in anonymous hash at HoH.pl line 69, <$TABLE> l +ine 5. Odd number of elements in anonymous hash at HoH.pl line 69, <$TABLE> l +ine 5. Odd number of elements in anonymous hash at HoH.pl line 69, <$TABLE> l +ine 5. Odd number of elements in anonymous hash at HoH.pl line 69, <$TABLE> l +ine 5. Odd number of elements in anonymous hash at HoH.pl line 69, <$TABLE> l +ine 5. Odd number of elements in anonymous hash at HoH.pl line 69, <$TABLE> l +ine 5. Odd number of elements in anonymous hash at HoH.pl line 69, <$TABLE> l +ine 5. Odd number of elements in anonymous hash at HoH.pl line 69, <$TABLE> l +ine 5. Odd number of elements in anonymous hash at HoH.pl line 69, <$TABLE> l +ine 5.

The data looks like this.

ProFam_645021567 (645021567),7.5e-206,682.6 (643773583),0.00018, +21.7 (643372890),0.00031,21.0 ProFam_645021568 (645021568),0,1218.3 (646523692),8.7e-59,200.0 + (649985079),1.9e-54,185.7 (643773996),7.5e-54,183.7 (6435316 +52),3.4e-51,175.0 (638130252),3.5e-51,174.9 (637982001),8.8e-51 +,173.6 (648039431),5.1e-48,164.5 (643372781),8.4e-48,163.8 ( +648040011),2.2e-47,162.4 (643772569),2.5e-47,162.2 (650562740), +9.4e-19,67.9 (638132465),0.00036,19.8 ProFam_645021569 (645021569),3.2e-261,866.1 (650562741),6.2e-47, +161.1 (646523691),4.9e-43,148.3 (643772570),1.8e-42,146.4 (6 +48040010),4e-41,142.0 (649985056),2.7e-38,132.6 (637982002),1.5 +e-36,127.0 (638130253),1e-32,114.3 (643372785),1.3e-32,114.0 + (643528419),5.3e-22,79.0 (648043902),1.1e-07,31.9

Here is the code I wrote.

my %HoH; open (my $TABLE,"<", "table") || die$!; while (<$TABLE>) { my $line = $_; chomp $line; if ($line =~/^ProFam/) { my @target = (); @target = split('\t', $line); #splits proteintable based on ta +bs my $name = $target[0]; #stores the protein family name shift (@target); #shifts the protein family name off the top o +f the name foreach my $p (@target) { #cut up $p if ($p =~/\(([\d]+)\)\,(.*)/) { $HoH{$name}{$1}={$2}; } } } }

I'm trying to store the ProFam_# as the name of the nested hash. I want the number in parentheses to represent the key and the data following it to be the value. Thanks in advance!

Also if you could point me to some good resources of HoH, it would be much appreciate!

Replies are listed 'Best First'.
Re: Hashes of Hashes Error
by Your Mother (Archbishop) on Nov 09, 2011 at 18:40 UTC

    Looks like this is the problem–

    {$2}

    The braces create an anonymous hash(ref) and hashes require pairs; { key => val } would be fine, for example, but you are only giving the equivalent of { key }, hence the odd number error.

      Would this fix my problem?

      $HoH{$name}{$1}={$1=>$2};

        It really depends on what you want to do with the data. Probably what you want is no hash at the end; guessing.

        $HoH{$name}{$1} = $2;