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,


In reply to Re: Counting Problem by Athanasius
in thread 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.