in reply to Selecting successive lines

I would read each record complete rather than one line at a time, and to keep it simple, would push scores into an anonymous array per student. Then just iterate over each student and do the math.

use List::Util qw( sum ); my %student; local $/ = ''; while( <DATA> ) { push @{$student{$1}}, $2 and next if m/\A([^\n]+).+?(\d+)\n?\Z/s; warn "Invalid record #$.: <<$_>>\n"; } while( my( $name, $scores ) = each %student ) { print "$name: Average ", sum( @{$scores} ) / @$scores, "\n"; } __DATA__ Jack Student ID - 12445 Math Score - 45 Jill Student ID - 234254 Math Score - 90 Jack Student ID -12445 Math Score2 - 33 Jill Student ID - 234254 Math Score2 - 10

Dave