my @source = qw( title text1 text2 text3 text4 title text1 text2 text3 ); my $i = 0; my @records = (); foreach my $sour (@source) { if ($sour eq 'title') { $records[$i] = { title => $sour }; $i++; } else { push @{ $records[$i - 1]->{value} }, $sour; } } use Data::Dumper; print Dumper \@records;