Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Finding Duplicates and Deleting in a Complex Data Structure

by hdb (Monsignor)
on Sep 05, 2014 at 13:21 UTC ( [id://1099667]=note: print w/replies, xml ) Need Help??


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}++; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1099667]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-25 23:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found