Hi Wise Monks,
I've got this application at work which begs for the following data structure - a hash of hashes of arrays. And I just do not know the syntax for populating such a data structure, much less printing it out.
The following is sort of a test bed. First, a data file of made up data for which I want to populate four arrays.
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 the following is a code attempt (well, one of quite a few, actually):
#!/usr/bin/perl -w
#use strict
open (TEST,">test.txt");
my ($team,$points,$tallies,$scores,$metrics);
my %teamStats = ();
open (STATS,"<testData.txt");
while (<STATS>)
{
@stuff = split(/\s+/,$_);
$team = $stuff[0];
$points = $stuff[1];
$tallies = $stuff[2];
$scores = $stuff[3];
$metrics = $stuff[4];
unless ($teamStats{$team})
{
$teamStats{$team} = {};
}
&fillTeamHash($teamStats{$team},$points,$tallies,$scores,$metrics)
+;
}
#while(($key, $value) = each(%HASH)) {
foreach $team (keys %teamStats)
{
print "\n\nTeam:\t$team\n";
#@pointsArray = $teamStats{$team}{'pointsArray'};
#foreach $item (@pointsArray)
#foreach $item ($teamStats{$team}{'pointsArray'})
#{
# print "$item\n";
#}
}
close (TEST);
sub fillTeamHash
{
my ($teamHashPtr,$points,$tallies,$scores,$metrics) = @_;
#$pointsArray = $$teamHashPtr{'pointsArray'};
#$talliesArray = $$teamHashPtr{'talliesArray'};
#$scoresArray = $$teamHashPtr{'scoresArray'};
#$metricsArray = $$teamHashPtr{'metricsArray'};
#print "In sub:\t$points\n";
#push (@$pointsArray,$points);
push (@$$teamHashPtr{'pointsArray'},$points);
#foreach $item (@$pointsArray)
foreach $item (@$$teamHashPtr{'pointsArray'},$points)
{
#$size = @$pointsArray;
$size = @$$teamHashPtr{'pointsArray'};
print TEST "Array Size:\t$size\t";
print TEST "Printing points array:\t$item\n";
}
#push (@$talliesArray,$tallies);
#push (@$scoresArray,$scores);
#push (@$metricsArray,$metrics);
}
If someone could tell me the syntax for such a problem, great. If someone could explain the WHY of the correct syntax, even better!
Thanks In Advance,
Tony (o2)
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.