How to convert a Perl hash-of-hashes to a more flexible data structure? -
In a quick and dirty Perl script, I have such a data structure:
Code> $ tax_revenue {YEAR} {STATE} {GOVLEV} {TAX} = IntegerThe hash key assumes value like this:
YEAR: 1 9 00 .. 2000 State: AK, AL, ... WY GOVLEV: State, local tax: type of tax (income, sales, etc.)
In addition, hash keys are unique . For example, any other value for the
tax
parameter falls below the value for other values.I am starting a medium-sized project working with this data and I would like to implement it, in a more flexible way, the data structure should not be available to me yet all data-retrieval functionality, but Here are some examples:
# Specify the parameter in any order tax_revie (qw (1902 WY state property)); Tax_rev (QW (state property 1902 WY)); # Use the named parameter. Tax_reve (year => 1902, state => 'WY', govlev = & gt; state ', tax = & gt; property'); Use wildcards to get a list of # values. # For example, state tax revenues for all states in 1902. Kar_rev (QW (1920 × State property));
My initial inclination was to store the data as a hash of hash and create one or more utility functions (possibly as part of a square) to get the values Had to do. But then I wonder if there is a better strategy - in addition to any kind of palm, there is no way to store the underlying data Any advice about the approach to this problem will be appreciated.
If you want a pure Pearl implementation, you can create an array of hashes:
my @taxdata = ({year => 1902, state => 'wy', level = & gt; 'state', type = & gt; property ', zodiac = & Gt; 500}, # ...); My @ matches = jeep {$ _- & gt; {Year} == 1902 & amp; Amp; $ _- & gt; {Level} eq 'state' & amp; Amp; $ _- & gt; {Type} eq 'property'} @ texata;
If you want to run arbitrary questions against it, but if you want to be able to get specific records, then it is flexible.
A better solution might be to have a database with a table where each row contains the fields you have listed, then you can write a SQL query to extract the data according to the arbitrary criteria. You can use the module to handle connections.