I am growing the following data structure from a flat file. The structure is as follows:
my @Divisions = qw(ABER BERF CECC DADD); my @rows; my %AG; my $Rec= {}; my %positions; my $csv = Text::CSV_XS->new ({ binary => 1, auto_diag => 1},encoding = +> "utf-8"); #CSV file with comma delimited data open my $fh1, "<", "test.csv" or die "test.csv: $!"; while (my $row = $csv->getline ($fh1)) { # do something with @$row if ($row->[12]) { push @rows, $row; } else { push @rows, $row; } } close $fh1 or die "data.csv: $!"; foreach my $rec (@rows) { foreach my $dept (@Divisions) { if ($rec->[14] =~ /^$dept/ && $rec->[15] =~ /^A\W[1-5]|B\W +[1-2]/) { my $Rec = { SECTION=>$rec->[0], GRADE=>strip_hyphen($rec->[1]), POSITION=>$rec->[2], NAME=>invert_name($rec->[3]), AGE =>convert_date($rec->[4]), GENDER=>$rec->[5], }; push @{$AG{$rec->[10]}},$Rec; } } } foreach my $A (sort keys %AG) { foreach my $p (@{$AG{$A}}) { print $p->{'GRADE'}," ", $p->{'NAME'}," ",$p->{'POSITION'},$p- +>{'AGE'}," ",$p->{'GENDER'}, "\n"; } }
Output of sample data structure using Dump:
VAR1 = 'ABER - Advanced Technologies'; $VAR2 = [ { 'NAME' => 'J. Green', 'DATE_OF_BIRTH' => '8/18/1959', 'SECTION' => 'ABER', 'POSITION' => 'DIRECTOR', 'AGE' => 55, 'GRADE' => 'B2' } ]; $VAR3 = 'BERF - Satellite Research'; $VAR4 = [ { 'NAME' => 'P. Smith', 'DATE_OF_BIRTH' => '12/11/1957', 'SECTION' => 'BERF', 'POSITION' => 'CHIEF', 'AGE' => 56, 'GRADE' => 'B1' }, { 'NAME' => 'R. Forest', 'DATE_OF_BIRTH' => '1/18/1954', 'SECTION' => 'BERF', 'POSITION' => 'SENIOR OFFICER', 'AGE' => '60 GREEN', 'GRADE' => 'A5' }, { 'NAME' => 'R.Forest', 'DATE_OF_BIRTH' => '03/09/1964', 'SECTION' => 'BERF', 'POSITION' => 'SENIOR OFFICER', 'AGE' => 'Vacant', 'GRADE' => 'A5' }, { 'NAME' => 'K. King', 'DATE_OF_BIRTH' => '8/9/1960', 'SECTION' => 'BERF', 'POSITION' => 'SENIOR OFFICER', 'AGE' => 54, 'FEMALE' => '', 'GRADE' => 'A5' }, ];

What I need to do is to count the number of duplicate GRADE keys (i.e. B1, A5,A4,etc.) and then to delete the GRADE key value so that it does not appear in the output if there is more than 1 GRADE key value of the same type.

Expected Output:

B2, J. Green, DIRECTOR,55,M B1,P.Smith,CHIEF,54,M A5,R.Forest,SENIOR OFFICER,60,M K.King,SENIOR OFFICER,54, M (A5 is excluded because it appears more + than once) P.Turner,50, M (A5 is excluded because it appears more than once)

Any pointers would really be appreciated.

In reply to Finding Duplicates and Deleting in a Complex Data Structure by GuiPerl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.