如何将Perl哈希散列转换为更灵活的数据结构? [英] How to convert a Perl hash-of-hashes to a more flexible data structure?

查看:210
本文介绍了如何将Perl哈希散列转换为更灵活的数据结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  $ tax_revenue {YEAR} 

在快速而肮脏的Perl脚本中,我有一个数据结构, {STATE} {GOVLEV} {TAX} =整数

散列键假设像这样的值: p>

  YEAR:1900 .. 2000 
状态:AK,AL,... WY
GOVLEV:state,当地
税:税收类型(收入,销售等)

此外,哈希键是唯一的。例如, TAX 参数的值与另一个其他参数的值相冲突。



我正在开始使用这些数据的中型项目,我想以更灵活的方式实施数据结构。我不知道我需要的所有数据检索功能,但这里有一些例子:

 #指定参数以任何顺序。 
Tax_rev(qw(1902 WY state property));
Tax_rev(qw(state property 1902 WY));

#使用命名参数。
Tax_rev(year => 1902,state =>'WY',govlev =>'state',tax =>'property');

#使用通配符获取值列表。
#例如,1902年所有州的国有财产税收入。
Tax_rev(qw(1902 * state property));

我最初的倾向是将数据作为哈希哈希存储,并构建一个或更多的效用函数(可能作为类的一部分)来检索值。但是,我想知道是否有更好的策略 - 一些存储基础数据而不是哈希散列的方法。对于如何处理这个问题的任何建议将不胜感激。

解决方案

如果你想要一个纯粹的Perl实现,你可以建立一个数组的哈希值:

 我的@taxdata =(
{year => 1902,state =>'WY' ,level =>'state',type =>'property',amount => 500},
#...
);

我的@matches = grep {
$ _-> {year} == 1902&&
$ _-> {level} eq'state'&&
$ _-> {type} eq'property'
} @taxdata;

如果您想对其运行任意查询,则非常灵活,但如果希望能够获得特定记录。



更好的解决方案可能是具有单个表的数据库,其中每行包含您列出的字段。然后,您可以编写一个SQL查询,以根据任意条件提取数据。您可以使用 DBI 模块来处理连接。


In a quick-and-dirty Perl script, I have a data structure like this:

$tax_revenue{YEAR}{STATE}{GOVLEV}{TAX} = integer

The hash keys assume values like this:

YEAR: 1900 .. 2000
STATE: AK, AL, ... WY
GOVLEV: state, local
TAX: type of tax (income, sales, etc.)

In addition, the hash keys are unique. For example, no value for the TAX parameter collides with a value for another other parameter.

I am starting a medium-sized project working with this data and I would like to implement the data structure in a more flexible way. I don't know all of the data-retrieval functionality I will need yet, but here are some examples:

# Specify the parameters in any order.
Tax_rev( qw(1902 WY state property) );
Tax_rev( qw(state property 1902 WY) );

# Use named parameters.
Tax_rev(year => 1902, state => 'WY', govlev => 'state', tax => 'property');

# Use wildcards to obtain a list of values.
# For example, state property tax revenue in 1902 for all states.
Tax_rev( qw(1902 * state property) );

My initial inclination was to keep storing the data as a hash-of-hashes and to build one or more utility functions (probably as part of a class) to retrieve the values. But then I wondered whether there is a better strategy -- some way of storing the underlying data other than a hash-of-hashes. Any advice about how to approach this problem would be appreciated.

解决方案

If you want a pure Perl implementation, you could build an array of hashes:

my @taxdata = (
    { year => 1902, state => 'WY', level => 'state', type => 'property', amount => 500 },
    # ...
);

my @matches = grep {
    $_->{year}  == 1902    &&
    $_->{level} eq 'state' &&
    $_->{type}  eq 'property'
} @taxdata;

That's flexible if you want to run arbitrary queries against it, but slow if you want to be able to get to a specific record.

A better solution might be a database with a single table where each row contains the fields you listed. Then you could write an SQL query to extract data according to arbitrary criteria. You can use the DBI module to handle the connection.

这篇关于如何将Perl哈希散列转换为更灵活的数据结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆