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

I would like to read my result set from each of my sql execution and populate the result set into multi dimensional hash.

first SQL lookup to create: key1 = {"AAA", "BBB"}
second SQL lookup to create: key2 = {"ccc", "BBB"}
third SQL lookup to create: key3 = {"ddd", "BBB"}
last SQL lookup to create: key4 = {"eee", "BBB"}

How would I auto populate the multi dimensional hash for each sql lookup?

How would Iprint out the values of the hash key?

Thank you.

Replies are listed 'Best First'.
Re: Multidimensional Hash
by shemp (Deacon) on Aug 07, 2004 at 02:22 UTC
    could you restate the question, i think i can help, but im not sure what you're asking?
Re: Multidimensional Hash
by Prior Nacre V (Hermit) on Aug 07, 2004 at 02:40 UTC

    Assuming you are using DBI, the following template should give you a good starting point:

    my %results = (); my $query = qq(SELECT id, col1, col2 FROM some_table); my $sth = $dbh->prepare($query); while (my $rh_row = $sth->fetchrow_hashref) { my $id = delete $rh_row->{id}; $results{$id} = { %$rh_row }; }

    Regards,

    PN5

      Why reinventing (badly) the wheel?

      my $query = qq(SELECT id, col1, col2 FROM some_table); my $results = $dbh->selectall_hashref($query, 1);

      Read all the explanation in DBI and DBI Recipes.

        • The original question was somewhat vague in terms of requirements
        • I provided a generic template using methods that I knew would be available without knowing the version of DBI in use
        • You are at liberty to dislike my solution
        • Constructive criticism might have been a better approach

        Many Internet Service Providers (ISPs) take the "if it ain't broke, don't fix it" approach. For instance, the ISP I moved from almost a year ago still uses a version of DBI that does not support the selectall_hashref() method.

        Regards,

        PN5