@a = (['a',1],['a',2],['b',3],['b',4]); $last = ['', '']; for $i (0..$#a) { $this = $a[$i]; $next = defined $a[$i+1] ? $a[$i+1] : $last; $total += $this->[1]; if ($this->[0] ne $next->[0]) { print "$this->[0]: $total\n"; $total = 0; } }