#!C:\Perl64\bin\perl.exe use strict; use warnings; my $RECORD = [ { 'NAME' => 'J. Green', 'GRADE' => 'P2', 'POSITION' => 'HUMAN RES +OURCES OFFICER' }, { 'NAME' => 'P. Smith', 'GRADE' => 'P1','POSITION' => 'FORESTRY O +FFICER' }, { 'NAME' => 'T. Turner', 'GRADE' => 'P1','POSITION' => 'FORESTRY O +FFICER' }, { 'NAME' => 'K. Turner', 'GRADE' => 'P1','POSITION' => 'FORESTRY +OFFICER' }, { 'NAME' => 'R. Forest', 'GRADE' => 'P5' ,'POSITION' => 'SENIOR OF +FICER'}, { 'NAME' => 'R.Forest', 'GRADE' => 'P5' ,'POSITION' => 'SENIOR OF +FICER'}, { 'NAME' => 'K. King', 'GRADE' => 'P5','POSITION' => 'SENIOR OFF +ICER' }, { 'NAME' => 'K. King', 'GRADE' => 'P3' ,'POSITION' => 'JUNIOR OF +FICER'}, { 'NAME' => 'K. King', 'GRADE' => 'P3' ,'POSITION' => 'POLICY OF +FICER'}, { 'NAME' => 'K. King', 'GRADE' => 'P1' ,'POSITION' => 'GENERAL O +FFICER'}, { 'NAME' => 'K. King', 'GRADE' => 'DG' ,'POSITION' => 'SENIOR DI +RECTOR'}, { 'NAME' => 'K. King', 'GRADE' => 'ADG','POSITION' => 'JUNIOR OF +FICER' }, ]; my $previous_grade=''; my $previous_position=''; my %grade_count; my %position_count; my $C=0; for my $p ( sort { substr($$a{GRADE},0,1) cmp substr($$b{GRADE},0, +1) || substr($$b{GRADE},0,2) cmp substr($$a{GRADE},0,2)} @$RECORD) { #for my $p ( sort { substr($$a{GRADE},0,1) cmp substr($$b{GRADE},0, +1) || substr($$b{GRADE},0,2) cmp substr($$a{GRADE},0,2)} @{$AG{$A}}) + { my( $grade, $name,$position ) = ( $p->{'GRADE'}, $p->{'NAME'} +,$p->{'POSITION'} ); my $pp=""; my $gg=""; if ($grade eq $previous_grade && $position eq $previous_position ) { $position=" "; $gg=$grade=" "; $pp=$position; $grade_count{$previous_grade}++; $position_count{$previous_position}++; } else { $gg=$p->{'GRADE'}; $pp=$p->{'POSITION'}; $grade_count{$previous_grade}++; $position_count{$previous_position}++; } if($grade_count{$previous_grade} == 1 && $position_count{$previous_pos +ition} == 1) { $C=$grade_count{$previous_grade}; print "$C"," ",$gg," ",$pp," ",$p->{NAME},"\n"; $grade_count{$previous_grade}++; $position_count{$previous_position}++; } elsif($grade_count{$previous_grade} > 1 && $position_count{$previous_ +position} > 1) { $C=$grade_count{$previous_grade}; print $C," ",$gg," ",$pp," ",$p->{NAME},"\n"; } $previous_grade = $p->{'GRADE'}; $previous_position = $p->{'POSITION'}; }
I am having a problem resolving a counting issue. As of the time being, the code above outputs the following:
1 ADG JUNIOR OFFICER K. King 1 DG SENIOR DIRECTOR K. King 1 P5 SENIOR OFFICER R. Forest 1 R.Forest 3 K. King 4 P3 JUNIOR OFFICER K. King 1 P1 FORESTRY OFFICER P. Smith 1 T. Turner 3 K. Turner 4 P1 GENERAL OFFICER K. King
The program logic should output the following instead:
1 ADG JUNIOR OFFICER K. King 1 DG SENIOR DIRECTOR K. King 3 P5 SENIOR OFFICER R. Forest R.Forest K. King 1 P3 JUNIOR OFFICER K. King 3 P1 FORESTRY OFFICER P. Smith T. Turner K. Turner 1 P1 GENERAL OFFICER K. King

Every time a GRADE and POSITION are encountered the counter should increment. The total of the equal GRADEs and POSITIONs should only be output once.

Any ideas would be greatly appreciated.


In reply to Counting Problem 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.