Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Grep hash of hashes

by pimperator (Acolyte)
on Dec 02, 2014 at 07:36 UTC ( [id://1108924] : perlquestion . print w/replies, xml ) Need Help??

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

in anno MMXIV o monachis

I have this input file I want to turn into a multidimensional hash

NAME NUMBER COLOR GNAEUS 1 BLUE GNAEUS 1 RED FABIUS 1 BLUE FABIUS 2 YELLOW FABIUS 1 RED ....

This is how I am turning it into a hash of hashes

undef my %hash; open IN, $ARGV[0]; while(<IN>){ chomp $_; my ($name,$number,$color) = split /\t/, $_; $hash{$name}{$number}{$color}++; }

Here's some pseudo-code of what I'm trying to accomplish

my %returnedHash = grep / $hash{"GNAEUS"}{"1"} / keys %hash; foreach my $name (keys %returnedHash){ foreach my $number (keys %{$returnedHash{$name}}){ foreach my $color (keys %{$returnedHash{$name}{$number}}){ print $returnedHash{$name}{$number}{$color}; } } }

I'm at a loss at the moment. That's why I'm here. Please help me o'Monks

Replies are listed 'Best First'.
Re: Grep hash of hashes
by choroba (Cardinal) on Dec 02, 2014 at 09:27 UTC
    Do you only want to see the counts? I guess not, you also want to see the number and colour to which the count belongs.

    BTW, the main point of $_ is you don't have to write it. If there's no assingment in a my statement, there's no need to undef it.

    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Grep hash of hashes
by Anonymous Monk on Dec 02, 2014 at 08:06 UTC

    Anyway, my advice, see Data::Dive example, Data::Dump::dd() early and often, write real code, start with dd( \%hash ) output, and build your loops on top of that, when you get stuck, post your code

Re: Grep hash of hashes
by McA (Priest) on Dec 02, 2014 at 08:08 UTC

    Hi,

    why do you need a deeply nested structure returnedHash when you know that the first two levels of keys are constant? Why don't you simply collect the last level?

    McA

      You seemed to have missed the fabulous FABIUS in yellow, rendering the second level not being constant.

        It seems I've misunderstood the whole problem. But that's the problem when you don't describe the problem clearly. So, you see, nothing but problems... ;-)