使用另一个哈希值定义一个哈希值。
有没有办法在仅使用一种数据结构的情况下执行以下操作?
my %hash = (
"key1" => "value1",
"key2" => "value2",
"key3" => $hash{key1},
);
所以基本上,我想将哈希的键值设置为另一个键值。我已经尝试了上述语法,但收到以下警告:
Global symbol "%hash" requires explicit package name at ...
我假设这是因为我试图在实际创建哈希之前引用它。
Is there a way to do the following while using only one data structure?
my %hash = (
"key1" => "value1",
"key2" => "value2",
"key3" => $hash{key1},
);
So basically, I want to set a hash's key value to another key value. I've tried the above syntax but get the following warning:
Global symbol "%hash" requires explicit package name at ...
I'm assuming this is because I'm trying to reference the hash before it's actually been created.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这是因为,在使用
$hash{key1}
时,符号“hash”尚未添加到符号表中。您的包还不知道%hash
是什么,因为直到解析结束的“)”之后它才会被添加到符号表中 - 但它会遇到前面 1 行的符号。您需要提前预先声明%hash:
但是,虽然上面的代码可以编译,但是它不会按预期工作,因为尚未将任何内容分配给
%hash
$hash{key1}
被评估。您需要首先将其作为单独的语句进行分配:请注意,上面仅将
key1
中的值复制到key3
一次。此后它们就不再有任何联系/关联。如果您想为key1
和key3
起别名(例如让它们指向相同的值,而不是包含该值的副本),则此是可能的,但并非那么微不足道。您需要将其放入绑定哈希并编写自定义绑定处理程序以使其正确,或者让每个键的值成为引用。This is because, at the moment of using
$hash{key1}
, the symbol "hash" was not yet added to the symbol table. Your package doesn't yet know what%hash
is, because it won't be added to the symbol table until AFTER the closing ")" is parsed - yet it will encounter the symbol 1 line earlier.You need to pre-declare %hash in advance:
However, while the above will compile, it will NOT work as expected, since nothing has been assigned to
%hash
yet when$hash{key1}
is evaluated. You need to assign it first, as a separate statement:Please note that the above merely copies the value from
key1
intokey3
, once. They are in no way linked/associated after that. If you want to aliaskey1
andkey3
(e.g. have them point to the same value, instead of contain a copy of the value), this is possible but not nearly as trivial. You need to make it into a tied hash and write custom tie handlers to get it right, or have each key's value be a reference.如果您希望 $hash{key1} 成为 $hash{key3} 的别名,那么可以使用 数据::别名。
$hash{key1} 和 $hash{key3} 现在将引用单个值,并且对一个值的更新将在另一个值中可见。
If you want $hash{key1} to be an alias of $hash{key3}, then this can be accomplished easily using Data::Alias.
$hash{key1} and $hash{key3} will now refer to a single value, and updates to one will be visible in the other.
您可以使用核心模块
Hash::Util
访问低级hv_store
例程,该例程可以将哈希值别名在一起。它看起来不像使用 Data::Alias 那样干净,但它已经安装了。如果您不希望别名在初始赋值之后持续存在,那么您可以
在初始声明之后编写以下行:并跳过使用
hv_store
。You can use the core module
Hash::Util
to access the low levelhv_store
routine which can alias values of the hash together. It isn't quite as clean looking as usingData::Alias
but it is already installed.if you dont want the aliasing to persist past the initial assignment, then you can just write the line:
after your initial declaration and skip using
hv_store
.为什么不创建自己的函数:
为了演示一些功能:
lit:
前缀涵盖了“如果我真的想要传递一个非-reference as'ref:so-and-so'
? 它在回答时也是递归的,“如果我非常需要创建一个值 'lit:xzy' 怎么办?我已经做到了这一点,并且我还祝福对
Lit
类或类似内容的传递数据的引用。然后在
make_hash
例程中,您只需检查ref( $value ) eq 'Ref'
即可。并像下面这样指定它:有很多方法可以让 Perl 表现得像你希望的那样。
Why not make your own function:
To demonstrate some of the power:
The
lit:
prefix covers the case of "What if I really wanted to pass a value that is a non-reference as'ref:so-and-so'
? It also is recursive in answering, "What if I direly need to make a value 'lit:xzy'.I've done this and I've also blessed a reference to a passed piece of data to a
Lit
class or something along those lines.And then in the
make_hash
routine you'd just check forref( $value ) eq 'Ref'
. And specify it like the following:There are many ways to make Perl act like you wish it did.
创建要分配给哈希的值列表时,
%hash
不包含任何内容,因为您尚未将该列表分配给哈希。事实上,在创建要分配给散列的值列表时,
%hash
并不存在,因为分配先评估其右侧,然后再评估其左侧。这就是为什么你会收到严格错误。您可能喜欢的一些解决方案:
%hash
doesn't contain anything when creating the list of values to assign to the hash, since you haven't assigned the list to the hash yet.In fact,
%hash
doesn't exist when creating the list of values to assign to the hash since assignments evaluate their RHS before their LHS. That's why you're getting a strict error.Some solutions you might like:
您可以说
只要引用的任何键在引用之前出现在列表中即可。
You could say
as long as any keys referred to appear in the list before they are referenced.