是否可以在其中存储有关散列的信息?我的意思是,不需要以普通的方式将信息添加到散列中,这会影响键、值等。
问题是,我正在将一个twod_array读入散列,但希望在不影响遍历散列的方式的情况下将顺序存储在原始数组中。
举个例子:
my @the_keys=keys %the_hash;不应返回有关哈希顺序的信息。
有没有办法在哈希表中存储元数据?
发布于 2012-07-02 20:29:07
您可以使用tie mechanism存储任意元数据。使用不影响标准哈希接口的包存储的最小示例:
package MetadataHash;
use Tie::Hash;
use base 'Tie::StdHash';
use Scalar::Util qw(refaddr);
our %INSERT_ORDER;
sub STORE {
my ($h, $k, $v) = @_;
$h->{$k} = $v;
push @{ $INSERT_ORDER{refaddr $h} }, $k;
}
1;package main;
tie my %h, 'MetadataHash';
%h = ( I => 1, n => 2, d => 3, e => 4 );
$h{x} = 5;
# %MetadataHash::INSERT_ORDER is (9042936 => ['I', 'n', 'd', 'e', 'x'])
print keys %h;
# 'enIxd'发布于 2012-07-02 19:18:11
好吧,我想人们总是可以使用Tie::Hash::Indexed:
use Tie::Hash::Indexed;
tie my %hash, 'Tie::Hash::Indexed';
%hash = ( I => 1, n => 2, d => 3, e => 4 );
$hash{x} = 5;
print keys %hash, "\n"; # prints 'Index'
print values %hash, "\n"; # prints '12345'https://stackoverflow.com/questions/11292539
复制相似问题