If you're not sure what a data structure looks like, you can use modules like Data::Dumper or Data::Dump::Streamer (see How can I visualize my complex data structure? for examples). If you're not sure how to navigate through it, perldsc has some good information. perlref and perlreftut may also help if you are not familiar with references.
That said, it looks like you've got a few problems in your code. When simplified, it looks like this:
First, it looks like you're overwriting $pending{correct_answer} each time, and all of the wrong answers are ending up in the same array (@{$pending{wrong}}). If I can read your intent correctly, I think you need something like $pending{$id}{correct_answer} and @{$pending{$id}{wrong}}.my @questions; my %pending; while($sth->fetch) { 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;
Finally, I think you might want to reconsider the type of data structure that you are creating and returning. Something like the following might work:
A complex data structure like this should be returned as a reference: \%hash.%hash = ( $question_id => { correct_answer => $whatever, wrong_answers => [ $oops, $my_bad, ... ] },
Update 1: Wow, am I slow tonight.
Update 2: As we discussed in the cb, 1) you may also want to consider using a join and combining your queries into a single SQL statement and 2) the DBI fetchall_*ref methods may help you simplify the code.
HTH
In reply to Re: Array of Hashes Issue
by bobf
in thread Array of Hashes Issue
by Spidy
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |