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' } ] }, ];