http://qs321.pair.com?node_id=829264


in reply to HTML::Template nested loops, DBI/MySQL and map

Another way to do it, though not as slick, but more readable, IMHO:

my $stmt = 'SELECT title, id FROM stages_stage ORDER BY id'; my $stages = $dbh->selectall_arrayref($stmt, {Slice => {}}); $stmt = 'SELECT title, stage_id FROM stages_topic ORDER BY stage_id'; my $topics = $dbh->selectall_arrayref($stmt, {Slice => {}}); for my $i ( 0 .. $#$stages ) { $list->[$i]{'stage_title'} = $stages->[$i]{'title'}; $ictr = 0; while ( $topics->[$tctr]{'stage_id'} == $stages->[$i]{'id'} ) { $list->[$i]{'topics'}->[$ictr]{'topic_title'} = $topics->[$tctr] +{'title'}; $tctr++; $ictr++; } } Dumper: $VAR1 = [ { 'stage_title' => 'Visualize Your Life', 'topics' => [ { 'topic_title' => 'Your Mind' }, { 'topic_title' => 'Your Identity' }, { 'topic_title' => 'Your Security' } ] }, { 'stage_title' => 'Choose Your Buyer', 'topics' => [ { 'topic_title' => 'Yourself' }, { 'topic_title' => 'Employee(s)' }, { 'topic_title' => 'Co-Owner' }, { 'topic_title' => 'Family' }, { 'topic_title' => 'Third-Party' } ] }, ];

—Brad
"The important work of moving the world forward does not wait to be done by perfect men." George Eliot