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

Im new to perl so please try to be patient. I have a hash of a hash that, if a key fits a criteria, I push into an array. I then try to read the rest its elements from the array, but it fails. I read both the perl docs and Q&A and still cant figure it out. Please help. Thanks
The hash looks like %employee = ( $email { Name => $foo, Address => $foo2, Telephone=> $foo3},); foreach my $email (keys %employee){ if ($email=~/$foo/i){ push (@found, \$employee{$email});} foreach my $emp (@found){ if ($emp->{'Name'}=~/$foo/i){ print $emp->{'Name'};}}

Replies are listed 'Best First'.
Re: hash reference
by jasonk (Parson) on Feb 13, 2003 at 19:47 UTC

    The objects you are pushing into @found are not references to hashes, they are references to references to hashes, remove the backslash before $employee{$email} and it will work as you expect.

    This does what you seem to be looking for:

    #!/usr/bin/perl -w use strict; my %employee = ( email => { Name => 'bob', Address => 'blah', Telephone=> 'foo3' } ); my @found; foreach my $email (keys %employee){ if ($email=~/e/i) { push (@found, $employee{$email}); } } foreach my $emp (@found){ print $emp."\n"; if ($emp->{'Name'}=~/bob/i) { print $emp->{'Name'}."\n"; } }
Re: hash reference
by fuzzyping (Chaplain) on Feb 13, 2003 at 19:43 UTC
    You're missing the => operator for creating the hash within the parent hash.
    %employee = ( $email => { Name => $foo, Address => $foo2, Telephone=> $foo3 }, );
    -fp
      Thanks, but thats not it. I built my hash through a series of loops. I just wanted to show what it looks like. I have tested my hash and it is built correctly.