use strict; use warnings; my %totals; while (){ next if /^$/; my ($f,$s) = split /\|/,$_; $totals{$f}{f} += $f; $totals{$f}{s} += $s; } for my $k (sort { $totals{$b}{s} <=> $totals{$a}{s} } keys %totals) { print "Key = $k\n" print "Element 1: $total{$k}{f}\n"; print "Element 3: $total{$k}{s}\n"; }