Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: add values with hash

by DACONTI (Scribe)
on Mar 11, 2008 at 15:00 UTC ( [id://673524]=note: print w/replies, xml ) Need Help??


in reply to add values with hash

Hi steph,
in order to solve your problem I recommend you first to look at hashes of hashes and autovivification in some perl book.
(For ex. Programming Perl)
Personally, for problems of your kind, I like to use hash references like in the example below:
use strict; my $hash={}; $hash->{countries}->{USA}->{animals}->{fox}=1560; $hash->{countries}->{Italy}={}; while ( my ($country, $country_ref) = each %{$hash->{countries}} ) { print "$country\n"; print $country_ref->{animals}->{fox}."\n" if exists $country_ref->{animals}->{fox}; };
By building hashes in this way
$hash->{countries}->{USA}->{animals}->{fox}=1560;
I can easily write very complex hash structures
In addition if you look carefully at the example above, you will remark that there is this kind of convention:
metadata->data->'detail metadata'->'detail data'
. If you do like that you have a single structure to which you can add at any time new dimensions without loosing degrees of freedom.
It shouldnt be too difficult to match this strategy to your actual problem.
Best Regs,
Davide.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://673524]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2024-04-25 21:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found