my (@array); while(){ chomp $_; my @cols = split("\t", $_); my $data = join("\t", $cols[2], $cols[5]); push (@array, {$cols[0]=>$data}); # to keep multiple entries for the same key } close(IN); foreach my $key(@array){ foreach my $value(keys %$key){ print $value, "\t", $key->{$value}, "\n"; #here how can I join values for the same key? #similar to OUTPUT shown below } } INPUT p1 cis 10 p1 plat 20 p1 gls NA p2 cis 0 p2 NA 9 OUTPUT p1 cis,plat,gls 10,20,NA p2 cis,NA 0,9