本文介绍了Perl 可以将数组引用存储为散列键吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下事项:

use strict;
use Data::Dumper;
my $hash={['one','two']=>[1,2]};
print Dumper($hash);
=for comment
prints....
$VAR1 = {
          'ARRAY(0x35358)' => [
                               1,
                               2
                              ]
         };
=cut

作为替代方案,散列中的键可以被构造为one\ttwo",然后我可以根据制表符分隔键的元素(在程序的后期处理数据时).

As an alternative, the key in the hash can be constrcuted as "one\ttwo" and then I can separate out the elements of the key based on tab delimiter (in latter part of the program while munging the data).

关于如何将键存储为数组引用的任何建议?

Any advice on how to store the key as a array reference?

推荐答案

不,普通的(非tied)Perl 哈希可以将字符串作为键.其他任何东西——arrayrefs、对象等等——如果用作散列键都会被字符串化,这使得散列键不能作为你最初拥有的任何非字符串东西使用.

No, a normal (non-tied) Perl hash can only have strings as keys. Anything else - arrayrefs, objects, whatever - will be stringified if used as a hash key, which leaves the hash key unusable as whatever non-string thing you originally had.

Hash::MultiKey 使用 tie 的魔法来避开这个限制.

Hash::MultiKey uses the magic of tie to sidestep this restriction.

这篇关于Perl 可以将数组引用存储为散列键吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 18:45