Hi guys, I have finally put together a small script that when implemented into a larger context will read data from a flat file and processed from output to Excel using Excel::Writer::XLSX. In the meantime, I am having difficulty accessing the data from the Array of Arrays. I trying to split the colon delimited data, however, the output is incorrect. I have stored each line of data using the $save scalar that concatenates each line from the foreach loop and pushes the results into an array. The data structure is provided as can be seen. To make a long story short, I would like to access each line of data and each individual data element. I am able to output correctly, however, I do not know how to repopulate the array to be accessed later on for further processing. Please note that I am a beginner.
Here is the code:
#!C:\Perl64\bin\perl.exe
use strict;
use warnings;
use Data:umper;
my %AG = (
GAHD => [ {
NAME => 'K. Long',
POSITION => 'SENIOR OFFICER',
GRADE => 'P5',
COUNTRY=>'ITALY',
PWD=>'Y',
DATE_OF_BIRTH=>'4/12/1944',
AGE => 45,
FEMALE=>'*',
VACANT=>'YELLOW'
},
{
NAME => 'J. Buber',
POSITION => 'CHIEF',
GRADE => 'D1',
COUNTRY=>'ITALY',
PWD=>'Y',
DATE_OF_BIRTH=>'4/12/1944',
AGE => 45,
FEMALE=>'*',
VACANT=>'YELLOW'
},
{
NAME => 'M. Amsi',
POSITION => 'SENIOR ANIMAL HEALTH OFFICER',
GRADE => 'P5',
COUNTRY=>'ITALY',
PWD=>'Y',
DATE_OF_BIRTH=>'4/12/1944',
AGE => 45,
FEMALE=>'*',
VACANT=>'YELLOW'
},
{
NAME => 'E. Xenu',
POSITION => 'SENIOR OFFICER',
GRADE => 'P5',
COUNTRY=>'ITALY',
PWD=>'Y',
DATE_OF_BIRTH=>'4/12/1944',
AGE => 45,
FEMALE=>'*',
VACANT=>'YELLOW'
},
],
GAGD => [
{
NAME => 'P. Cheru',
POSITION => 'ANIMAL PRODUCTION OFFICER',
GRADE => 'P4',
COUNTRY=>'ITALY',
PWD=>'Y',
DATE_OF_BIRTH=>'4/12/1944',
AGE => 45,
FEMALE=>'*',
VACANT=>'YELLOW'
},
{
NAME => 'B. Burns',
POSITION => 'ANIMAL PRODUCTION OFFICER',
GRADE => 'P4',
COUNTRY=>'ITALY',
PWD=>'Y',
DATE_OF_BIRTH=>'4/12/1944',
AGE => 45,
FEMALE=>'*',
VACANT=>'YELLOW'
},
{
NAME => 'R. Mung',
POSITION => 'ANIMAL PRODUCTION OFFICER',
GRADE => 'P4',
COUNTRY=>'ITALY',
PWD=>'Y',
DATE_OF_BIRTH=>'4/12/1944',
AGE => 45,
FEMALE=>'*',
VACANT=>'YELLOW'
},
{
NAME => 'B. Scherf',
POSITION => 'ANIMAL PRODUCTION OFFICER',
GRADE => 'P4',
COUNTRY=>'ITALY',
PWD=>'Y',
DATE_OF_BIRTH=>'4/12/1944',
AGE => 45,
FEMALE=>'*',
VACANT=>'YELLOW'
},
{
NAME => 'I. Hoffmann',
POSITION => 'CHIEF',
GRADE => 'P5',
AGE => 45,
FEMALE=>'*',
VACANT=>'YELLOW',
COUNTRY=>'ITALY',
PWD=>'Y',
DATE_OF_BIRTH=>'4/12/1944',
}
]);
my %positions;
my $counts = 0;
my %grades;
my $rec;
my @new2;
my $count=0;
my $c;
my $save;
for my $division (keys %AG) {
print "**$division**\n";
my %group;
my %group2;
for my $person (@{ $AG{$division} }) {
my $position = join ':', @{ $person }{qw{GRADE POSITION}};
push @{ $group{$position} },$rec=[$person->{NAME},$person->{COUNTRY},
$person->{DATE_OF_BIRTH},$person->{AGE},$person->{FEMALE},$person->{VACANT},$person->{PWD}]
}
for my $position ( sort { substr($a, 0, 1) cmp substr($b, 0, 1) || substr($b, 0, 2) cmp substr($a, 0, 2) } keys %group) {
$position =~ /(\w\d)\W(.*)/g;
my $pos=$1;
my $title=$2;
my $c=scalar @{ $group{$position} };
$save.=[$c.':'.$pos.':'.$title];
print scalar @{ $group{$position} }.":".$pos.':'.$title.':';
foreach my $key ( @{ $group{$position} }) {
print join ":",$key->[0],$key->[1],$key->[2],$key->[3],$key->[4],$key->[5],$key->[6],"\n";
$save.=[':'.$key->[0].':'.$key->[1].':'.$key->[2].':'.$key->[3].':'.$key->[4].':'.$key->[5].':'.$key->[6]."\n"];
}
}
# print Dumper @new2;
# exit;
push(@new2,$save);
}
foreach my $l_output (@new2) {
my($a,$b,$c,$e,$f,$g,$h,$i) = split (/:/,$l_output);
print join " ",$a,$b,$c,$e,$f,$g,$h,$i,"\n";
}
Output
**GAGD**
15:CHIEF:I. Hoffmann:ITALY:4/12/1944:45:*:YELLOW:Y:
44:ANIMAL PRODUCTION OFFICER. Cheru:ITALY:4/12/1944:45:*:YELLOW:Y:
B. Burns:ITALY:4/12/1944:45:*:YELLOW:Y:
R. Mung:ITALY:4/12/1944:45:*:YELLOW:Y:
B. Scherf:ITALY:4/12/1944:45:*:YELLOW:Y:
**GAHD**
11:CHIEF:J. Buber:ITALY:4/12/1944:45:*:YELLOW:Y:
25:SENIOR OFFICER:K. Long:ITALY:4/12/1944:45:*:YELLOW:Y:
E. Xenu:ITALY:4/12/1944:45:*:YELLOW:Y:
15:SENIOR ANIMAL HEALTH OFFICER:M. Amsi:ITALY:4/12/1944:45:*:YELLOW:Y:
ARRAY(0x2f90db8)ARRAY(0x2f90db8)ARRAY(0x2f90e00)ARRAY(0x2f90e00)ARRAY(0x2f90de8)ARRAY(0x2f90410)ARRAY(0x2f90e00)
ARRAY(0x2f90db8)ARRAY(0x2f90db8)ARRAY(0x2f90e00)ARRAY(0x2f90e00)ARRAY(0x2f90de8)ARRAY(0x2f90410)ARRAY(0x2f90e00)ARRAY(0x2f90cc8)ARRAY(0x2f90cc8)ARRAY(0x2f90cb0)ARRAY(0x2f90cb0)ARRAY(0x2f8edf8)ARRAY(0x2f90cc8)ARRAY(0x2f90cc8)
Here is the code:
#!C:\Perl64\bin\perl.exe
use strict;
use warnings;
use Data:umper;
my %AG = (
GAHD => [ {
NAME => 'K. Long',
POSITION => 'SENIOR OFFICER',
GRADE => 'P5',
COUNTRY=>'ITALY',
PWD=>'Y',
DATE_OF_BIRTH=>'4/12/1944',
AGE => 45,
FEMALE=>'*',
VACANT=>'YELLOW'
},
{
NAME => 'J. Buber',
POSITION => 'CHIEF',
GRADE => 'D1',
COUNTRY=>'ITALY',
PWD=>'Y',
DATE_OF_BIRTH=>'4/12/1944',
AGE => 45,
FEMALE=>'*',
VACANT=>'YELLOW'
},
{
NAME => 'M. Amsi',
POSITION => 'SENIOR ANIMAL HEALTH OFFICER',
GRADE => 'P5',
COUNTRY=>'ITALY',
PWD=>'Y',
DATE_OF_BIRTH=>'4/12/1944',
AGE => 45,
FEMALE=>'*',
VACANT=>'YELLOW'
},
{
NAME => 'E. Xenu',
POSITION => 'SENIOR OFFICER',
GRADE => 'P5',
COUNTRY=>'ITALY',
PWD=>'Y',
DATE_OF_BIRTH=>'4/12/1944',
AGE => 45,
FEMALE=>'*',
VACANT=>'YELLOW'
},
],
GAGD => [
{
NAME => 'P. Cheru',
POSITION => 'ANIMAL PRODUCTION OFFICER',
GRADE => 'P4',
COUNTRY=>'ITALY',
PWD=>'Y',
DATE_OF_BIRTH=>'4/12/1944',
AGE => 45,
FEMALE=>'*',
VACANT=>'YELLOW'
},
{
NAME => 'B. Burns',
POSITION => 'ANIMAL PRODUCTION OFFICER',
GRADE => 'P4',
COUNTRY=>'ITALY',
PWD=>'Y',
DATE_OF_BIRTH=>'4/12/1944',
AGE => 45,
FEMALE=>'*',
VACANT=>'YELLOW'
},
{
NAME => 'R. Mung',
POSITION => 'ANIMAL PRODUCTION OFFICER',
GRADE => 'P4',
COUNTRY=>'ITALY',
PWD=>'Y',
DATE_OF_BIRTH=>'4/12/1944',
AGE => 45,
FEMALE=>'*',
VACANT=>'YELLOW'
},
{
NAME => 'B. Scherf',
POSITION => 'ANIMAL PRODUCTION OFFICER',
GRADE => 'P4',
COUNTRY=>'ITALY',
PWD=>'Y',
DATE_OF_BIRTH=>'4/12/1944',
AGE => 45,
FEMALE=>'*',
VACANT=>'YELLOW'
},
{
NAME => 'I. Hoffmann',
POSITION => 'CHIEF',
GRADE => 'P5',
AGE => 45,
FEMALE=>'*',
VACANT=>'YELLOW',
COUNTRY=>'ITALY',
PWD=>'Y',
DATE_OF_BIRTH=>'4/12/1944',
}
]);
my %positions;
my $counts = 0;
my %grades;
my $rec;
my @new2;
my $count=0;
my $c;
my $save;
for my $division (keys %AG) {
print "**$division**\n";
my %group;
my %group2;
for my $person (@{ $AG{$division} }) {
my $position = join ':', @{ $person }{qw{GRADE POSITION}};
push @{ $group{$position} },$rec=[$person->{NAME},$person->{COUNTRY},
$person->{DATE_OF_BIRTH},$person->{AGE},$person->{FEMALE},$person->{VACANT},$person->{PWD}]
}
for my $position ( sort { substr($a, 0, 1) cmp substr($b, 0, 1) || substr($b, 0, 2) cmp substr($a, 0, 2) } keys %group) {
$position =~ /(\w\d)\W(.*)/g;
my $pos=$1;
my $title=$2;
my $c=scalar @{ $group{$position} };
$save.=[$c.':'.$pos.':'.$title];
print scalar @{ $group{$position} }.":".$pos.':'.$title.':';
foreach my $key ( @{ $group{$position} }) {
print join ":",$key->[0],$key->[1],$key->[2],$key->[3],$key->[4],$key->[5],$key->[6],"\n";
$save.=[':'.$key->[0].':'.$key->[1].':'.$key->[2].':'.$key->[3].':'.$key->[4].':'.$key->[5].':'.$key->[6]."\n"];
}
}
# print Dumper @new2;
# exit;
push(@new2,$save);
}
foreach my $l_output (@new2) {
my($a,$b,$c,$e,$f,$g,$h,$i) = split (/:/,$l_output);
print join " ",$a,$b,$c,$e,$f,$g,$h,$i,"\n";
}
Output
**GAGD**
15:CHIEF:I. Hoffmann:ITALY:4/12/1944:45:*:YELLOW:Y:
44:ANIMAL PRODUCTION OFFICER. Cheru:ITALY:4/12/1944:45:*:YELLOW:Y:
B. Burns:ITALY:4/12/1944:45:*:YELLOW:Y:
R. Mung:ITALY:4/12/1944:45:*:YELLOW:Y:
B. Scherf:ITALY:4/12/1944:45:*:YELLOW:Y:
**GAHD**
11:CHIEF:J. Buber:ITALY:4/12/1944:45:*:YELLOW:Y:
25:SENIOR OFFICER:K. Long:ITALY:4/12/1944:45:*:YELLOW:Y:
E. Xenu:ITALY:4/12/1944:45:*:YELLOW:Y:
15:SENIOR ANIMAL HEALTH OFFICER:M. Amsi:ITALY:4/12/1944:45:*:YELLOW:Y:
ARRAY(0x2f90db8)ARRAY(0x2f90db8)ARRAY(0x2f90e00)ARRAY(0x2f90e00)ARRAY(0x2f90de8)ARRAY(0x2f90410)ARRAY(0x2f90e00)
ARRAY(0x2f90db8)ARRAY(0x2f90db8)ARRAY(0x2f90e00)ARRAY(0x2f90e00)ARRAY(0x2f90de8)ARRAY(0x2f90410)ARRAY(0x2f90e00)ARRAY(0x2f90cc8)ARRAY(0x2f90cc8)ARRAY(0x2f90cb0)ARRAY(0x2f90cb0)ARRAY(0x2f8edf8)ARRAY(0x2f90cc8)ARRAY(0x2f90cc8)