Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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' ] };

In reply to Re: Dereference an Array of Hashes by Marshall
in thread Dereference an Array of Hashes by dirtdog

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2024-04-23 14:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found