in reply to Finding Duplicates and Deleting in a Complex Data Structure

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

Replies are listed 'Best First'.
Re^2: Finding Duplicates and Deleting in a Complex Data Structure
by GuiPerl (Acolyte) on Sep 05, 2014 at 15:09 UTC
    Thanks a million. By the way, how would I count the number of B2, A5s etc?

      You could use a hash and count within the loop:

      my $previous_grade = ''; my %grade_count; 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; $grade_count{$grade}++; }