Hello hungrysumo79, and welcome to the Monastery!

In addition to the problem identified by beech — using an assignment (=) where a comparison (== or, in this case, eq) is needed — there is a serious problem in the way the hash %names is populated. These lines:

while (my @row = $sth->fetchrow()) { $names{$row[0]}{$row[1]} = [$row[2],$row[3],$row[4],$row[5]]; }

overwrite the hash entry for $names{$row[0]}{$row[1]} each time a new entry is found, so in the end only the last entry is retained in the hash. Rather than an assignment, you need to push each entry onto an anonymous array:

while (my @row = $sth->fetchrow()) { push @{ $names{ $fields[0] }{ $fields[1] } }, [ @fields[2 .. 5] ]; }

(Note the use of an array slice to reduce the amount of typing required.) This adds another level to the data structure, so you need to be careful when dereferencing. The following example should give you the idea:

use strict; use warnings; my %names = ( X => { A => [ [ qw( Gay Deceiver Head Honcho ) ], [ qw( Chill Blaine Corporate Pawn ) ], [ qw( Tuesday Next Government Agent ) ], [ qw( Harry Hoo Freelance PI ) ], ], B => [ [ qw( Wiley Coyote Acme Addict ) ], [ qw( Gay Divorcee Party Animal ) ], ], }, Y => { A => [ [ qw( Maxwell Smart Agent 86 ) ], ], }, ); for my $key1 (keys %names) { for my $key2 (keys %{ $names{$key1} }) { for my $row_ref (@{ $names{$key1}{$key2} }) { if ($row_ref->[0] eq 'Gay') { print 'Found: ', $row_ref->[0], ' ', $row_ref->[1], "\ +n"; } } } }

Output:

14:21 >perl 1541_SoPW.pl Found: Gay Divorcee Found: Gay Deceiver 14:22 >

By the way, you should always use warnings. And why do the work yourself when the database can do it for you? Why not change your query to something along these lines?

my $sth = $dbh->prepare("SELECT * FROM employees WHERE firstname = 'Gay'" );

:-)

Update: minor edits.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re: multi level hash terror - if statement to access specific elements by Athanasius
in thread multi level hash terror - if statement to access specific elements by hungrysumo79

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.