Spidy has asked for the wisdom of the Perl Monks concerning the following question:
For a recent project, we need to retrieve every question, it's single correct answer, and all of the wrong answers associated with it. This is the code that we have used to do this, and store it into an array of hashes:
my @questions; my %pending; $sth = $dbh->prepare("SELECT id, question FROM questions"); $sth->execute(); $sth->bind_columns(\@pending{qw(id question)}); while($sth->fetch) { my $sth = $dbh->prepare("SELECT answer, is_correct FROM answer +s WHERE to_question = ?"); $sth->execute($pending{id}); $sth->bind_columns(\@pending{qw(answer is_correct)}); while($sth->fetch) { if($pending{is_correct}) { $pending{correct_answer} = $pending{answer}; } else { ## Add to array of wrong answers push @{$pending{wrong}}, $pending{answer}; } } push @questions, %pending; } return @questions;
However, upon retrieving the returned array within my caller script, I am unsure as to how I'm supposed to loop through this array. Can anyone give me some pointers in the right direction?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Array of Hashes Issue
by bobf (Monsignor) on Mar 03, 2007 at 06:49 UTC | |
|
Re: Array of Hashes Issue
by GrandFather (Saint) on Mar 03, 2007 at 06:39 UTC | |
|
Re: Array of Hashes Issue
by thezip (Vicar) on Mar 03, 2007 at 06:34 UTC | |
by GrandFather (Saint) on Mar 03, 2007 at 06:44 UTC | |
|
Re: Array of Hashes Issue
by jmmistrot (Sexton) on Mar 05, 2007 at 09:12 UTC |