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?
In reply to Array of Hashes Issue by Spidy
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |