for my $key (keys %$hash1) { my @Array; push (@Array, @{$hash1->{$key}}[0..3]); my $ref=$hash2->{$key}; push (@Array, $ref?@$ref[2,3]:('','')); $result{$key}=\@Array; }