use warnings; use strict; my %h; chomp( my @arr = split /\s+?/, ); my $key; for (@arr) { if ( !/[[:digit:]]/ ) { $key = $_; } else { if ( $h{$key} ) { $h{$key} += $_; } else { $h{$key} = $_ } } } print $_, q[ ], $h{$_}, q[ ], for sort keys %h; __DATA__ a 1 b 3 c 5 a 2