GuiPerl has asked for the wisdom of the Perl Monks concerning the following question:
I am having a problem resolving a counting issue. As of the time being, the code above outputs the following:#!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'}; }
The program logic should output the following instead: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
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Counting Problem
by Corion (Patriarch) on Sep 10, 2014 at 11:51 UTC | |
|
Re: Counting Problem
by roboticus (Chancellor) on Sep 10, 2014 at 14:01 UTC | |
by GuiPerl (Acolyte) on Sep 10, 2014 at 15:01 UTC | |
by roboticus (Chancellor) on Sep 10, 2014 at 20:23 UTC | |
by Anonymous Monk on Sep 11, 2014 at 12:16 UTC | |
by roboticus (Chancellor) on Sep 11, 2014 at 15:36 UTC | |
| |
|
Re: Counting Problem
by Athanasius (Archbishop) on Sep 10, 2014 at 13:56 UTC |