in reply to Counting Problem
Hello GuiPerl,
In programming, getting the right data structure is usually more than half the battle. In this case, you want to output the data sorted by (1) grade and (2) position, with each unique grade/position pair prefixed by a count of the number of persons with this grade and position. So you need a data structure that will allow you to easily access the data in the desired order and calculate the count.
In Perl, there is a maxim: when in doubt, use a hash. What is needed here is actually a bit more complicated: a hash of hashes of arrays (HoHoA). Easier to demonstrate than to try to explain, so here is how I would begin:
#! perl use strict; use warnings; use Data::Dump; my $RECORD = [ { NAME => 'J. Green', GRADE => 'P2', POSITION => 'HUMAN RESOURCE +S OFFICER' }, { NAME => 'P. Smith', GRADE => 'P1', POSITION => 'FORESTRY OFFIC +ER' }, { NAME => 'T. Turner', GRADE => 'P1', POSITION => 'FORESTRY OFFIC +ER' }, { NAME => 'K. Turner', GRADE => 'P1', POSITION => 'FORESTRY OFFIC +ER' }, { NAME => 'R. Forest', GRADE => 'P5', POSITION => 'SENIOR OFFICER +' }, { NAME => 'R.Forest', GRADE => 'P5', POSITION => 'SENIOR OFFICER +' }, { NAME => 'K. King', GRADE => 'P5', POSITION => 'SENIOR OFFICER +' }, { NAME => 'K. King', GRADE => 'P3', POSITION => 'JUNIOR OFFICER +' }, { NAME => 'K. King', GRADE => 'P3', POSITION => 'POLICY OFFICER +' }, { NAME => 'K. King', GRADE => 'P1', POSITION => 'GENERAL OFFICE +R' }, { NAME => 'K. King', GRADE => 'DG', POSITION => 'SENIOR DIRECTO +R' }, { NAME => 'K. King', GRADE => 'ADG', POSITION => 'JUNIOR OFFICER +' }, ]; my %grades; push @{ $grades{$_->{GRADE}}{$_->{POSITION}} }, $_->{NAME} for @$RECOR +D; dd \%grades;
Output:
23:51 >perl 1008_SoPW.pl { ADG => { "JUNIOR OFFICER" => ["K. King"] }, DG => { "SENIOR DIRECTOR" => ["K. King"] }, P1 => { "FORESTRY OFFICER" => ["P. Smith", "T. Turner", "K. Turner" +], "GENERAL OFFICER" => ["K. King"], }, P2 => { "HUMAN RESOURCES OFFICER" => ["J. Green"] }, P3 => { "JUNIOR OFFICER" => ["K. King"], "POLICY OFFICER" => ["K. K +ing"] }, P5 => { "SENIOR OFFICER" => ["R. Forest", "R.Forest", "K. King"] }, } 23:51 >
Once you have the data in this form, it’s fairly straightforward to generate the desired output:
for my $key (sort { substr($a, 0, 1) cmp substr($b, 0, 1) || substr($b, 0, 2) cmp substr($a, 0, 2) } keys %grad +es) { for my $pos (sort { $a cmp $b } keys %{ $grades{$key} }) { my $names = $grades{$key}->{$pos}; my $count = scalar @$names; print $count, ' ', $key, ' ', $pos, ' ', $names->[0], "\n"; print ' ', $names->[$_], "\n" for 1 .. $#$names; } }
Output:
23:51 >perl 1008_SoPW.pl 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 1 P3 POLICY OFFICER K. King 1 P2 HUMAN RESOURCES OFFICER J. Green 3 P1 FORESTRY OFFICER P. Smith T. Turner K. Turner 1 P1 GENERAL OFFICER K. King 23:53 >
Hope that helps,
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
|
|---|