use strict; use warnings; open FILE1, '<', 'file1' or die ($!); my %file1 = map { split '\|', $_ } ; ## we now have A1=>'dog' in hash close FILE1; for(sort keys %file1){ print "$_\n"; print join('|',$_,$file1{$_}),'|',"\n"; }