my %items; my @items; while ($row = $sth->fetch_hashref()) { our $item; local *item = \( $items{ $row->{item_key} } ); if (!$item) { $item = { item_key => $row->{item_key}, item_description => $row->{item_description}, item_summary => $row->{item_summary}, actions => [], }; push @items, $item; } push @{ $item->{action} }, { key => $row->{action_key}, summary => $row->{action_summary}, }; }