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

Hi Monks, I am searching through a hash with multiple values per key, and printing out the contents. However, the second value is failing to print, although I can't see what is wrong with my syntax! I have used Data::Dumper to check that each key has two values, which is does. Can anyone see an error? Here is my code and some Data:Dumper contents. Cheers!

my %hash = map {($part1[$_] => [$part2[$_], $gp_ids[$_]])}0..$#part1; #print "$#island_ids $#nonisland_ids $#plasmid_ids\n"; my %map; @map{@island_ids} = ("island") x @island_ids; @map{@nonisland_ids} = ("nonisland") x @nonisland_ids; @map{@plasmid_ids} = ("plasmid") x @plasmid_ids; while (my($k,$v) = each %hash) { print "$map{$k}-$map{$v->[0]} $map{$v->[1]}\n"; } use Data::Dumper; print Dumper \%hash; # output from hash: island-plasmid plasmid-island plasmid-island island-nonisland plasmid-island # output from Dumper: 'c6679893-6679264 ' => [ '39687-39872 ', '1' ], 'c4702714-4702079 ' => [ 'c78072-77437 ', '138' ],

Replies are listed 'Best First'.
Re: hashes with multiple values per key
by puudeli (Pilgrim) on Apr 04, 2006 at 10:27 UTC
    Your %hash is empty, thus nothing shows up in the Dumper, maybe you didn't post enough code? It is hard to try the code you provided, since it does not work as it is.

    Also, you are slicing your %map (extremely confusing variable name) which simply assigns an anonymous list of one string to each hash key in hash %map. Your code is essentially the same as:

    @island_ids = qw(1 2 3); ($map{1}, $map{2}, $map{3}) = ( ("island_id"), ("island_id"), ("island +_id") );
    Thus, you have only one value in each key. Maybe you intended to slice the %hash instead?
    --
    seek $her, $from, $everywhere if exists $true{love};
Re: hashes with multiple values per key
by lima1 (Curate) on Apr 04, 2006 at 09:57 UTC
    try use warnings.

    what's the output of Dumper \%map?

        well, after you filled that map. it can't be empty as you get some output.