Your question: "array of hashes" did not match up with the code. There is that and then there is Hash of Array.
I'll show both ways with some slightly different coding. The Array of Hash roughly corresponds to the C "array of structure". If you don't need to randomly access different managers, then this is a good way - works out well if you are always processing managers as a group. Also maintains the order of the records if that winds up being needed.
When building these things, also note that curly braces {} and brackets [] mean "allocate new anon hash or array memory". The value of such a thing is a reference to either newly allocated hash or an array memory. This allows the code to look a bit "cleaner", or at least I think so in this particular case.
What kind of data struct you use depends upon how you intend to process the data. And there are variants like Hash of Hash, etc.
Oh, @{$managers{$manager}}, the extra set of curly braces are needed, (instead of @$managers{$manager} to make it clear what the @ is operating upon. Here the @ operates on the value of $managers{$manager}, which is an array reference. A "Hash of Array" means a hash key that has as a value, an array reference. You have to use the hash key to get the hash value and then deference that hash value (which is a reference to an array) to get the actual "data".
#!/usr/bin/perl -w
use strict;
use Data::Dumper;
my @mans = qw / Joe Mike Rich /;
my @tot_updates = qw /12 7 17 /;
my @tot_events = qw /45 14 10 /;
my @managers;
while (@mans)
{
push @managers, { manager => shift @mans,
tot_updates => shift @tot_updates,
tot_events => shift @tot_events
};
}
foreach my $href (@managers)
{
print "$href->{manager} $href->{tot_updates} ".
"$href->{tot_events}\n";
}
print Dumper \@managers;
####### or ###### Hash of Array ###
@mans = qw / Joe Mike Rich /;
@tot_updates = qw /12 7 17 /;
@tot_events = qw /45 14 10 /;
my %managers;
while (@mans)
{
$managers{shift @mans} = [ shift @tot_updates,
shift @tot_events];
}
foreach my $manager (keys %managers)
{
print "$manager @{$managers{$manager}}\n";
}
print Dumper \%managers;
__END__
## Array of Hash ###
Joe 12 45
Mike 7 14
Rich 17 10
$VAR1 = [
{
'tot_events' => '45',
'tot_updates' => '12',
'manager' => 'Joe'
},
{
'tot_events' => '14',
'tot_updates' => '7',
'manager' => 'Mike'
},
{
'tot_events' => '10',
'tot_updates' => '17',
'manager' => 'Rich'
}
];
########## Hash of Array ####
Joe 12 45
Rich 17 10
Mike 7 14
$VAR1 = {
'Joe' => [
'12',
'45'
],
'Rich' => [
'17',
'10'
],
'Mike' => [
'7',
'14'
]
};
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.