use strict; use warnings; use Data::Dumper; my %teams = (); while ( <DATA> ) { chomp; my ($team, $points, $tallies, $scores, $metrics) = split; push @{ $teams{$team}->{points} }, $points; push @{ $teams{$team}->{tallies} }, $tallies; push @{ $teams{$team}->{scores} }, $scores; push @{ $teams{$team}->{metrics} }, $metrics; } my $dd = Data::Dumper->new([\%teams], [qw{*teams}]); print $dd->Dumpxs(); __END__ packers 4 40 400 4000 patriots 6 62 436 3987 colts 8 74 892 7666 bears 9 88 912 9550 packers 3 44 410 4200 patriots 7 66 510 3800 colts 10 77 910 7000 bears 11 88 1010 9410 packers 2 36 385 4105 patriots 4 58 500 3700 colts 9 75 900 7500 bears 8 95 1017 10200 packers 4 37 388 4378 patriots 6 55 440 4987 colts 8 80 843 8210 bears 9 101 890 9998 packers 7 40 400 4000 patriots 11 62 436 3987 colts 7 74 892 7666 bears 12 88 912 9550 packers 2 48 422 4320 patriots 3 54 510 3765 colts 6 72 812 7500 bears 7 86 899 9430
and here's the output
%teams = ( 'colts' => { 'scores' => [ '892', '910', '900', '843', '892', '812' ], 'tallies' => [ '74', '77', '75', '80', '74', '72' ], 'points' => [ '8', '10', '9', '8', '7', '6' ], 'metrics' => [ '7666', '7000', '7500', '8210', '7666', '7500' ] }, 'patriots' => { 'scores' => [ '436', '510', '500', '440', '436', '510' ], 'tallies' => [ '62', '66', '58', '55', '62', '54' ], 'points' => [ '6', '7', '4', '6', '11', '3' ], 'metrics' => [ '3987', '3800', '3700', '4987', '3987', '3765' ] }, 'packers' => { 'scores' => [ '400', '410', '385', '388', '400', '422' ], 'tallies' => [ '40', '44', '36', '37', '40', '48' ], 'points' => [ '4', '3', '2', '4', '7', '2' ], 'metrics' => [ '4000', '4200', '4105', '4378', '4000', '4320' ] }, 'bears' => { 'scores' => [ '912', '1010', '1017', '890', '912', '899' ], 'tallies' => [ '88', '88', '95', '101', '88', '86' ], 'points' => [ '9', '11', '8', '9', '12', '7' ], 'metrics' => [ '9550', '9410', '10200', '9998', '9550', '9430' ] } );
The top-level hash is keyed by team and there is for each team a sub-hash for each of points, tallies, scores and metrics, the values in each case being an array of said points etc. Thus, for points, we want $teams{$team}->{points} to be an array so enclosing it in the @{ ... } de-references the value in that sub-hash to get at the array so that the push can add another element to the end. Note also that the anonymous array will be created (autovivified) if it didn't exist already.
I probably haven't explained this very well. Reply with more questions if it isn't clear.
Cheers,
JohnGG
In reply to Re: Need Help With Syntax for (Complicated for Me) Data Structure
by johngg
in thread Need Help With Syntax for (Complicated for Me) Data Structure
by o2bwise
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |