Instead of removing duplicate grades you should just suppress the printing of them. If you first sort your data by grades and then not print repeated grades, you should get what you want. It could look like this (based on simplified data):
use strict;
use warnings;
my $data = [
{ 'NAME' => 'J. Green', 'GRADE' => 'B2' },
{ 'NAME' => 'P. Smith', 'GRADE' => 'B1' },
{ 'NAME' => 'R. Forest', 'GRADE' => 'A5' },
{ 'NAME' => 'R.Forest', 'GRADE' => 'A5' },
{ 'NAME' => 'K. King', 'GRADE' => 'A5' },
];
my $previous_grade = '';
for my $item ( sort { $a->{'GRADE'} cmp $b->{'GRADE'} } @$data ) {
my( $grade, $name ) = ( $item->{'GRADE'}, $item->{'NAME'} );
print $grade eq $previous_grade ? ( ' ' x ( length( $grade )+1 ) )
+ : "$grade,";
print "$name\n";
$previous_grade = $grade;
}
gives you
A5,R. Forest
R.Forest
K. King
B1,P. Smith
B2,J. Green