我正在尝试处理我使用Data :: Dumper输出的perl数据结构
$VAR1 = 'GAHD'; $VAR2 = [ { 'COUNTRY' => 'US', 'NAME' => 'K. Long', 'DATE_OF_BIRTH' => '7/27/1957', 'POSITION' => 'SENIOR OFFICER', 'AGE' => 57, 'GRADE' => 'P5' }, { 'COUNTRY' => 'US', 'NAME' => 'J. Buber', 'DATE_OF_BIRTH' => '12/11/1957', 'POSITION' => 'CHIEF', 'GRADE' => 'D1' }, { 'COUNTRY' => 'US', 'NAME' => 'M. Amsi', 'DATE_OF_BIRTH' => '1/1/1957', 'POSITION' => 'SENIOR ANIMAL HEALTH OFFICER', 'AGE' => 57, 'GRADE' => 'P5' }, { 'COUNTRY' => 'US', 'NAME' => 'E. Xenu', 'DATE_OF_BIRTH' => '8/31/1964', 'POSITION' => 'SENIOR OFFICER', 'AGE' => 50, 'GRADE' => 'P5' }, ]; $VAR3 = 'GAGD'; $VAR4 = [ { 'COUNTRY' => 'US', 'NAME' => 'P. Cheru', 'DATE_OF_BIRTH' => '6/18/1966', 'POSITION' => 'ANIMAL PRODUCTION OFFICER', 'AGE' => 48, 'GRADE' => 'P4' }, { 'COUNTRY' => 'US', 'NAME' => 'B. Burns', 'DATE_OF_BIRTH' => '2/4/1962', 'POSITION' => 'ANIMAL PRODUCTION OFFICER', 'AGE' => 52, 'GRADE' => 'P4' }, { 'COUNTRY' => 'US', 'NAME' => 'R. Mung', 'DATE_OF_BIRTH' => '12/13/1968', 'POSITION' => 'ANIMAL PRODUCTION OFFICER', 'AGE' => 45, 'GRADE' => 'P4' }, { 'COUNTRY' => 'GERMANY', 'NAME' => 'B. Scherf', 'DATE_OF_BIRTH' => '8/31/1964', 'POSITION' => 'ANIMAL PRODUCTION OFFICER', 'AGE' => 50, 'GRADE' => 'P4' }, { 'COUNTRY' => 'GERMANY', 'NAME' => 'I. Hoffmann', 'DATE_OF_BIRTH' => '2/21/1960', 'POSITION' => 'CHIEF', 'AGE' => 54, 'GRADE' => 'P5' }, ];输出如下:
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 3 P1 FORESTRY OFFICER P. Smith T. Turner K. Turner 1 P1 GENERAL OFFICER K. King我想按分部计算GRADES和POSITIONS数。 这是我到目前为止汇总的代码:
#Push data read from a flat file and while loop push @{ $grades{ $_->{GRADE} }{ $_->{POSITION} } }, $_->{NAME} for @$AG; for my $key ( sort { substr( $a, 0, 1 ) cmp substr( $b, 0, 1 ) || substr( $b, 0, 2 ) cmp substr( $a, 0, 2 ) } keys %grades ) { 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; } }如果重复的POSITIONS和GRADES数据(即P1,高级官员)出现在另一个部门,代码将停止输出结果。
我不知道如何通过分区(即GAGD,GAGHD等)访问Hash of Hash,以便每个分区输出相同的GRADE和POSITION。
这是我真正需要的:
**GAGD** 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 3 P1 FORESTRY OFFICER P. Smith T. Turner K. Turner 1 P1 GENERAL OFFICER K. King **GAGHD** 1 P3 JUNIOR OFFICER P. Green 3 P1 FORESTRY OFFICER R. Brown F. Boo K. Church 1 P1 GENERAL OFFICER D. Peefer etc. etc.I am trying to process a perl data structure that I have outputted using Data::Dumper
$VAR1 = 'GAHD'; $VAR2 = [ { 'COUNTRY' => 'US', 'NAME' => 'K. Long', 'DATE_OF_BIRTH' => '7/27/1957', 'POSITION' => 'SENIOR OFFICER', 'AGE' => 57, 'GRADE' => 'P5' }, { 'COUNTRY' => 'US', 'NAME' => 'J. Buber', 'DATE_OF_BIRTH' => '12/11/1957', 'POSITION' => 'CHIEF', 'GRADE' => 'D1' }, { 'COUNTRY' => 'US', 'NAME' => 'M. Amsi', 'DATE_OF_BIRTH' => '1/1/1957', 'POSITION' => 'SENIOR ANIMAL HEALTH OFFICER', 'AGE' => 57, 'GRADE' => 'P5' }, { 'COUNTRY' => 'US', 'NAME' => 'E. Xenu', 'DATE_OF_BIRTH' => '8/31/1964', 'POSITION' => 'SENIOR OFFICER', 'AGE' => 50, 'GRADE' => 'P5' }, ]; $VAR3 = 'GAGD'; $VAR4 = [ { 'COUNTRY' => 'US', 'NAME' => 'P. Cheru', 'DATE_OF_BIRTH' => '6/18/1966', 'POSITION' => 'ANIMAL PRODUCTION OFFICER', 'AGE' => 48, 'GRADE' => 'P4' }, { 'COUNTRY' => 'US', 'NAME' => 'B. Burns', 'DATE_OF_BIRTH' => '2/4/1962', 'POSITION' => 'ANIMAL PRODUCTION OFFICER', 'AGE' => 52, 'GRADE' => 'P4' }, { 'COUNTRY' => 'US', 'NAME' => 'R. Mung', 'DATE_OF_BIRTH' => '12/13/1968', 'POSITION' => 'ANIMAL PRODUCTION OFFICER', 'AGE' => 45, 'GRADE' => 'P4' }, { 'COUNTRY' => 'GERMANY', 'NAME' => 'B. Scherf', 'DATE_OF_BIRTH' => '8/31/1964', 'POSITION' => 'ANIMAL PRODUCTION OFFICER', 'AGE' => 50, 'GRADE' => 'P4' }, { 'COUNTRY' => 'GERMANY', 'NAME' => 'I. Hoffmann', 'DATE_OF_BIRTH' => '2/21/1960', 'POSITION' => 'CHIEF', 'AGE' => 54, 'GRADE' => 'P5' }, ];The following is outputted:
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 3 P1 FORESTRY OFFICER P. Smith T. Turner K. Turner 1 P1 GENERAL OFFICER K. KingI would like to count the number of GRADES and POSITIONS by Division. Here is the code that I have put together thus far:
#Push data read from a flat file and while loop push @{ $grades{ $_->{GRADE} }{ $_->{POSITION} } }, $_->{NAME} for @$AG; for my $key ( sort { substr( $a, 0, 1 ) cmp substr( $b, 0, 1 ) || substr( $b, 0, 2 ) cmp substr( $a, 0, 2 ) } keys %grades ) { 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; } }The code will stop outputting results if duplicate POSITIONS and GRADES data (i.e. P1, Senior Officer) appear in another Division.
I do not know how to access the Hash of Hash by Division (i.e. GAGD, GAGHD,etc.) so that the same GRADEs and POSITIONs will be outputted per division.
Here is what I really need:
**GAGD** 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 3 P1 FORESTRY OFFICER P. Smith T. Turner K. Turner 1 P1 GENERAL OFFICER K. King **GAGHD** 1 P3 JUNIOR OFFICER P. Green 3 P1 FORESTRY OFFICER R. Brown F. Boo K. Church 1 P1 GENERAL OFFICER D. Peefer etc. etc.最满意答案
您似乎希望按部门哈希信息,然后按等级+位置计算和存储名称。 以下似乎对我有用:
#!/usr/bin/perl use warnings; use strict; use feature qw{ say }; my %grades = ( GAHD => [ { NAME => 'K. Long', POSITION => 'SENIOR OFFICER', GRADE => 'P5' }, { NAME => 'J. Buber', POSITION => 'CHIEF', GRADE => 'D1' }, { NAME => 'M. Amsi', POSITION => 'SENIOR ANIMAL HEALTH OFFICER', GRADE => 'P5' }, { NAME => 'E. Xenu', POSITION => 'SENIOR OFFICER', GRADE => 'P5' }, ], GAGD => [ { NAME => 'P. Cheru', POSITION => 'ANIMAL PRODUCTION OFFICER', GRADE => 'P4' }, { NAME => 'B. Burns', POSITION => 'ANIMAL PRODUCTION OFFICER', GRADE => 'P4' }, { NAME => 'R. Mung', POSITION => 'ANIMAL PRODUCTION OFFICER', GRADE => 'P4' }, { NAME => 'B. Scherf', POSITION => 'ANIMAL PRODUCTION OFFICER', GRADE => 'P4' }, { NAME => 'I. Hoffmann', POSITION => 'CHIEF', GRADE => 'P5' }, ]); for my $division (keys %grades) { say "**$division**"; my %group; for my $person (@{ $grades{$division} }) { my $position = join ' ', @{ $person }{qw{GRADE POSITION}}; push @{ $group{$position} }, $person->{NAME}; } for my $position (keys %group) { say join ' ', scalar @{ $group{$position} }, $position, $group{$position}[0]; my @remaining_names = @{ $group{$position} }; shift @remaining_names; say "\t$_" for @remaining_names; } say q(); }更新
如果您存储的信息多于数组引用中某个人的名称( push push @{ $group{$position} }, [ ... ]; ),则可以通过取消引用每个引用来检索它,例如在map :
say join ' ', scalar @{ $group{$position} }, $position, join "\n\t", map "@$_", @{ $group{$position} };It seems you want to hash the information by Division, then count and store names by grade + position. The following seems to work for me:
#!/usr/bin/perl use warnings; use strict; use feature qw{ say }; my %grades = ( GAHD => [ { NAME => 'K. Long', POSITION => 'SENIOR OFFICER', GRADE => 'P5' }, { NAME => 'J. Buber', POSITION => 'CHIEF', GRADE => 'D1' }, { NAME => 'M. Amsi', POSITION => 'SENIOR ANIMAL HEALTH OFFICER', GRADE => 'P5' }, { NAME => 'E. Xenu', POSITION => 'SENIOR OFFICER', GRADE => 'P5' }, ], GAGD => [ { NAME => 'P. Cheru', POSITION => 'ANIMAL PRODUCTION OFFICER', GRADE => 'P4' }, { NAME => 'B. Burns', POSITION => 'ANIMAL PRODUCTION OFFICER', GRADE => 'P4' }, { NAME => 'R. Mung', POSITION => 'ANIMAL PRODUCTION OFFICER', GRADE => 'P4' }, { NAME => 'B. Scherf', POSITION => 'ANIMAL PRODUCTION OFFICER', GRADE => 'P4' }, { NAME => 'I. Hoffmann', POSITION => 'CHIEF', GRADE => 'P5' }, ]); for my $division (keys %grades) { say "**$division**"; my %group; for my $person (@{ $grades{$division} }) { my $position = join ' ', @{ $person }{qw{GRADE POSITION}}; push @{ $group{$position} }, $person->{NAME}; } for my $position (keys %group) { say join ' ', scalar @{ $group{$position} }, $position, $group{$position}[0]; my @remaining_names = @{ $group{$position} }; shift @remaining_names; say "\t$_" for @remaining_names; } say q(); }Update
If you store more information than a name for a person in an array ref (push push @{ $group{$position} }, [ ... ];), you can then retrieve it by dereferencing each reference, for example in map:
say join ' ', scalar @{ $group{$position} }, $position, join "\n\t", map "@$_", @{ $group{$position} };
更多推荐
发布评论