CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Accessing Elements from a Hash/Array Structure

 



PerlGui
Novice

Sep 19, 2014, 11:26 AM

Post #1 of 2 (4812 views)
Accessing Elements from a Hash/Array Structure Can't Post

Hi guys,

I have put together I program, see below that outputs as follows:

**GAGD**
2 D1 CHIEF P. Hoffmann,45,FRANCE
R. Hoff,45,FRANCE
1 P5 OFFICER B. Scherf,45,FRANCE
1 P5 ANIMAL PRODUCTION OFFICER B. Burns,45,FRANCE
1 P2 ANIMAL PRODUCTION OFFICER P. Cheru,45,FRANCE
1 P1 ANIMAL PRODUCTION OFFICER R. Mung,45,FRANCE
**GAHD**
2 P5 SENIOR OFFICER K. Long,45,FRANCE
E. Xenu,45,FRANCE
1 P5 CHIEF J. Buber,45,FRANCE
1 P5 SENIOR ANIMAL HEALTH OFFICER M. Amsi,45,FRANCE

The program finds all records that have the same position and grade, counts them outputs and excludes if duplicates are found. Now what I wish to do is to access each element via the $position variable in order to access every element. What I have done thus far is to map the elements, however, what I would like is something like $group{$position}->{NAME},$group{$position}->{AGE},etc. I do not know how to do this. Any help would be greatly appreciated.

Completed Program

Code
#!C:\Perl64\bin\perl.exe 


use strict;
use warnings;
use feature qw{ say };


my %grades = (
GAHD => [ {
NAME => 'K. Long',
POSITION => 'SENIOR OFFICER',
GRADE => 'P5',
AGE => 45,
COUNTRY=>'FRANCE'
},
{
NAME => 'J. Buber',
POSITION => 'CHIEF',
GRADE => 'P5',
AGE => 45,
COUNTRY=>'FRANCE'
},
{
NAME => 'M. Amsi',
POSITION => 'SENIOR ANIMAL HEALTH OFFICER',
GRADE => 'P5',
AGE => 45,
COUNTRY=>'FRANCE',

},
{
NAME => 'E. Xenu',
POSITION => 'SENIOR OFFICER',
GRADE => 'P5',
AGE => 45,
COUNTRY=>'FRANCE'
},
],
GAGD => [
{
NAME => 'P. Cheru',
POSITION => 'ANIMAL PRODUCTION OFFICER',
GRADE => 'P2',
AGE => 45,
COUNTRY=>'FRANCE'
},
{
NAME => 'B. Burns',
POSITION => 'ANIMAL PRODUCTION OFFICER',
GRADE => 'P5',
AGE => 45,
COUNTRY=>'FRANCE'
},
{
NAME => 'R. Mung',
POSITION => 'ANIMAL PRODUCTION OFFICER',
GRADE => 'P1',
AGE => 45,
COUNTRY=>'FRANCE'
},
{
NAME => 'B. Scherf',
POSITION => 'OFFICER',
GRADE => 'P5',
AGE => 45,
COUNTRY=>'FRANCE'

},
{
NAME => 'P. Hoffmann',
POSITION => 'CHIEF',
GRADE => 'D1',
AGE => 45,
COUNTRY=>'FRANCE'
},
{
NAME => 'R. Hoff',
POSITION => 'CHIEF',
GRADE => 'D1',
AGE => 45,
COUNTRY=>'FRANCE'
},

]);


for my $division (keys %grades) {
print "**$division**\n";
my %group;
my %group2;
my $rec;
for my $person (@{ $grades{$division} }) {
my $position = join ' ', @{ $person }{qw{GRADE POSITION}};


push @{ $group{$position} },$rec=[$person->{NAME},$person->{AGE},$person->{COUNTRY}]

#push (@staff,$rec=[$person->{PWD},$person->{NAME},$person->{COUNTRY},$person->{DATE_OF_BIRTH},$person->{AGE},$person->{FEMALE},$person->{VACANT}]);


}
for my $position (sort { substr($a, 0, 1) cmp substr($b, 0, 1) ||
substr($b, 0, 2) cmp substr($a, 0, 2) }keys %group) {



print join ' ', scalar @{ $group{$position} },
$position,
join " ", map "\t$$_[0],$$_[1],$$_[2]\n", @{ $group{$position} };
}

}



wickedxter
User

Sep 19, 2014, 9:57 PM

Post #2 of 2 (4780 views)
Re: [PerlGui] Accessing Elements from a Hash/Array Structure [In reply to] Can't Post

This allows u to access the info you need in the loops that build $group hash


Code
#!Perl 

use strict;
use warnings;
use Data::Dumper;

my %grades = (
GAHD => [ {
NAME => 'K. Long',
POSITION => 'SENIOR OFFICER',
GRADE => 'P5',
AGE => 45,
COUNTRY=>'FRANCE'
},
{
NAME => 'J. Buber',
POSITION => 'CHIEF',
GRADE => 'P5',
AGE => 45,
COUNTRY=>'FRANCE'
},
{
NAME => 'M. Amsi',
POSITION => 'SENIOR ANIMAL HEALTH OFFICER',
GRADE => 'P5',
AGE => 45,
COUNTRY=>'FRANCE',

},
{
NAME => 'E. Xenu',
POSITION => 'SENIOR OFFICER',
GRADE => 'P5',
AGE => 45,
COUNTRY=>'FRANCE'
},
],
GAGD => [
{
NAME => 'P. Cheru',
POSITION => 'ANIMAL PRODUCTION OFFICER',
GRADE => 'P2',
AGE => 45,
COUNTRY=>'FRANCE'
},
{
NAME => 'B. Burns',
POSITION => 'ANIMAL PRODUCTION OFFICER',
GRADE => 'P5',
AGE => 45,
COUNTRY=>'FRANCE'
},
{
NAME => 'R. Mung',
POSITION => 'ANIMAL PRODUCTION OFFICER',
GRADE => 'P1',
AGE => 45,
COUNTRY=>'FRANCE'
},
{
NAME => 'B. Scherf',
POSITION => 'OFFICER',
GRADE => 'P5',
AGE => 45,
COUNTRY=>'FRANCE'

},
{
NAME => 'P. Hoffmann',
POSITION => 'CHIEF',
GRADE => 'D1',
AGE => 45,
COUNTRY=>'FRANCE'
},
{
NAME => 'R. Hoff',
POSITION => 'CHIEF',
GRADE => 'D1',
AGE => 45,
COUNTRY=>'FRANCE'
},

]);



my $group;

# grde -> position -> person(s)
for my $dev (keys %grades){

#loop thru the array of people
for my $people ($grades{$dev}){
#hash
for my $person (@{$people}){


push @{$group->{$person->{GRADE}}{$person->{POSITION}}}, {
#change this to add what data you want to print in the end
NAME => $person->{NAME},
AGE => $person->{AGE}
};


}
}


}

my $num_of_ppl;
#loop grade
for my $grade (keys %{$group}){
#loop position

for my $pos (keys %{$group->{$grade}}){

$num_of_ppl = scalar @{$group->{$grade}{$pos}};
print "$num_of_ppl $grade $pos ";


map {
print $_->{NAME} . '/' . $_->{AGE} if $num_of_ppl == 1;
print " $_->{NAME}" . '/' . $_->{AGE} if $num_of_ppl > 1;
} @{$group->{$grade}{$pos}};

print "\n";
}
print "\n";
}


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives