in reply to Re^4: Counting Problem
in thread Counting Problem

You could use slot 0 of your array to hold the count, and let the names occupy slots 1..n. That would give you a structure like:

my %AG = ( 'OfficeOfDir' => [ 5, # slot 0 holds count { GRADE=>'A', POSITION=>'Quarterback', NAME=>'Unitas, J.' }, { GRADE=>'B', POSITION=>'Nickleback', NAME=>'Elephino' }, ], 'OfficeOfTech' => [ 2, { GRADE=>'B2', POSITION=>'Secretary', NAME=>'BillTheGalacticHe +ro' }, { GRADE=>'C5', POSITION=>'Diplomat', NAME=>'StainlessSteelRat +' }, ], );

There are many alternatives, though. You need to come up with something that feels natural to you, and you find easy to work with in your code.

Also, I wasn't kidding when I mentioned:

...roboticus

When your only tool is a hammer, all problems look like your thumb.

Replies are listed 'Best First'.
Re^6: Counting Problem
by GuiPerl (Acolyte) on Sep 12, 2014 at 14:18 UTC
    Robitcus,

    Many thanks for all your guidance.

    One last question. When you are composing your data structure, I noticed that you are pushing 3 values (i.e.push @{ $grades{$_->{GRADE}}{$_->{POSITION}} }, $_->{NAME} for @$RECORD).

    How would I go about adding additional values into the @$grades hash such as $_{SECTION},$_{AGE} etc so that they are outputted as follows: GRADE, POSITION, NAME, SECTION, AGE etc.?

    I can't figure out how to add additional hash keys.

    Thanks.

      GuiPerl:

      If you use Dumper to print out the data structure, you can look at the structure to see what to do. For example:

      $VAR1 = ( # top level look at next item to see if array + or hash FOO=>{ # Key: top level is hash. { means next is ha +sh too BAR=>{ # another hash BAZ=>[ # square bracket starts an array { BIM=>1 }, # slot 0 contains a hash { BIM=>5 }, # slot 1 also contains a hash 'GEORGE', # slot 2 holds a string [3,6,9] # slot 3 holds an array }, }, }, FOE=>[ # FOE is key for an array ['a','b','c'], # slot 0 holds another array {FUM=>'FIE'}, # slot 1 holds a hash ], ); say $H{FOO}{BAR}{BAZ}[0]{BIM}; # should show 1 say $H{FOO}{BAR}{BAZ}[2]; # should show GEORGE say $H{FOO}{BAR}{BAZ}[3][1]; # should show 6 say $H{FOE}[0][2]; # should show c say $H{FOE}[1]{FUM}; # should show FIE

      So look at your Dumper output, and look at the characters used to separate the levels. You'll want to use the same characters to get to the data you want. Does that help clear things up?

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.