my %r; for(@unpairedSidBinder){ my($sid,$foo)=split(/\|/); push(@{$r{$sid}//=[]},$foo); } print(map("$_|".join(' ',@{$r{$_}})."\n",keys(%r)));