use Data::Dumper; while (){ next if /^$/; ($f,$s) = split /\|/,$_; $h{$f} += $f; $h->{$f}{'s'} += $s; } for my $k (sort {$h{$b} <=> $h{$a} } keys %h){ print "Key = $k\nElement 1: $h{$k}\nElement 3: $h->{$k}{'s'}\n"; }; #print Dumper{%{$h}}; __DATA__ 1|2 2|4 2|4 4|3 4|5 5|1 5|2