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:

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;
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}}.

Second, you flatten the %pending hash when you push it onto @questions. If you really want to use an array (see below), try pushing a hash reference instead: push( @questions, \%pending ) (note that this would require you to tighten the scope of %pending).

Third, you are returning an array, but an array reference might be better: 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:

%hash = ( $question_id => { correct_answer => $whatever, wrong_answers => [ $oops, $my_bad, ... ] },
A complex data structure like this should be returned as a reference: \%hash.

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

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.