in reply to merge with multiple row with same key

Probably a better thought out solution could be made, but I think this may be what you're after.
#!/usr/bin/perl use strict; use warnings; my %hash = ( 'abc,def' => { ',,,item4' => 1, 'item1,,,' => 1, ',item2,,' => 1 }, 'kln,mno' => { ',,,item4' => 1, 'item1,,,' => 1 }, ); my %new_hash; for my $key (keys %hash) { my @array; for (keys %{ $hash{$key}}) { my ($lead_comma,$item) = /^(,*)([^,]+)/; $array[ length $lead_comma ] = $item; } $new_hash{$key} = \@array; } use Data::Dumper; print Dumper \%new_hash; __END__ output from Data::Dumper $VAR1 = { 'abc,def' => [ 'item1', 'item2', undef, 'item4' ], 'kln,mno' => [ 'item1', undef, undef, 'item4' ] };