Hi fellows, 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 push 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. Here is the code:
#!C:\Perl64\bin\perl.exe use strict; use warnings; use Data::Dumper; 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 %grou +p) { $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 from above:
**GAGD** 1:P5:CHIEF:I. Hoffmann:ITALY:4/12/1944:45:*:YELLOW:Y: 4:P4:ANIMAL PRODUCTION OFFICER:P. 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** 1:D1:CHIEF:J. Buber:ITALY:4/12/1944:45:*:YELLOW:Y: 2:P5:SENIOR OFFICER:K. Long:ITALY:4/12/1944:45:*:YELLOW:Y: E. Xenu:ITALY:4/12/1944:45:*:YELLOW:Y: 1:P5: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(0x2f9 +0cc8)ARRAY(0x2f90cb0)ARRAY(0x2f90cb0)ARRAY(0x2f8edf8)ARRAY(0x2f90cc8) +ARRAY(0x2f90cc8)
Any pointers would be greatly appreciated. In fact, I need to know how to dereference the array.

In reply to Accessing Array of Arrays 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.