使用数组值进行 Perl 哈希的最佳实践是什么?
解决这个问题的最佳实践是什么?
if (... )
{
push (@{$hash{'key'}}, @array ) ;
}
else
{
$hash{'key'} ="";
}
将一个元素存储为数组或将一个元素存储为散列中的双引号是一种不好的做法吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我不确定我是否理解你的问题,但我现在会按字面意思回答它......
问题的关键是数组或哈希采用标量值......所以你需要引用你的数组或散列并将其用作值。
请参阅 perlref 和 perlreftut 了解更多信息。
编辑:是的,您可以添加空字符串作为某些键的值和其他键的引用(数组或散列,甚至标量,类型团/文件句柄或其他标量。无论哪种方式)。它们仍然是标量。
您需要查看
ref
函数来计算了解如何消除引用类型和普通标量之间的歧义。I'm not sure I understand your question, but I'll answer it literally as asked for now...
The crux of the problem is that arrays or hashes take scalar values... so you need to take a reference to your array or hash and use that as the value.
See perlref and perlreftut for more information.
EDIT: Yes, you can add empty strings as values for some keys and references (to arrays or hashes, or even scalars, typeglobs/filehandles, or other scalars. Either way) for other keys. They're all still scalars.
You'll want to look at the
ref
function for figuring out how to disambiguate between the reference types and normal scalars.使用显式数组引用可能更简单:
实际上,执行上述操作并使用
push
会产生相同的数据结构:这给出:
使用显式数组引用使用更少的字符,并且比
更容易阅读>push @{$hash{'key'}}, @array
构造,IMO。编辑:对于您的
else{}
块,分配空字符串可能不太理想。跳过if-else
构造,稍后当您访问哈希中的值时,执行if( Defined( $hash{'key '} ) )
检查。这更接近标准 Perl 习惯用法,并且您不会浪费内存在散列中存储空字符串。相反,您必须使用 ref() 来找出值中包含哪些类型的数据,这比仅进行定义性检查不太清楚。
It's probably simpler to use explicit array references:
Actually, doing the above and using
push
result in the same data structure:This gives:
Using explicit array references uses fewer characters and is easier to read than the
push @{$hash{'key'}}, @array
construct, IMO.Edit: For your
else{}
block, it's probably less than ideal to assign an empty string. It would be a lot easier to just skip theif-else
construct and, later on when you're accessing values in the hash, to do aif( defined( $hash{'key'} ) )
check. That's a lot closer to standard Perl idiom, and you don't waste memory storing empty strings in your hash.Instead, you'll have to use
ref()
to find out what kind of data you have in your value, and that is less clear than just doing a defined-ness check.我不确定您的目标是什么,但有几件事需要考虑。
首先,如果要存储数组,您想要存储对原始值的引用还是原始值的副本?在任何一种情况下,我都倾向于避免取消引用语法并尽可能获取引用:
接下来,您是否要添加到已经存在的值,即使它是单个值?如果您要拥有数组值,即使您只有一个元素,我也会将所有值设为数组。然后,您不必决定要做什么,并且可以删除特殊情况:
这可能是您的“最佳实践”答案,这通常是删除尽可能多的特殊情况和额外逻辑的技术。当我在模块中执行此类操作时,我通常会使用一个
add_value
方法来封装这种魔法,这样我就不必多次查看或键入它。如果散列键中已经有非引用值,也很容易修复:
如果您已经有想要在数组中的非数组引用值,则可以执行类似的操作。也许您希望匿名哈希成为数组元素之一:
I'm not sure what your goal is, but there are several things to consider.
First, if you are going to store an array, do you want to store a reference to the original value or a copy of the original values? In either case, I prefer to avoid the dereferencing syntax and take references when I can:
Next, do you want to add to the values that exist already, even if it's a single value? If you are going to have array values, I'd make all the values arrays even if you have a single element. Then you don't have to decide what to do and you remove a special case:
That might be your "best practice" answer, which is often the technique that removes as many special cases and extra logic as possible. When I do this sort of thing in a module, I typically have a
add_value
method that encapsulates this magic where I don't have to see it or type it more than once.If you already have a non-reference value in the hash key, that's easy to fix too:
If you already have non-array reference values that you want to be in the array, you do something similar. Maybe you want an anonymous hash to be one of the array elements:
处理修订后的符号:
我们可以立即看出您没有遵循保护新手(和专家!)免受自己错误影响的标准建议。您使用的是符号引用,这不是一个好主意。
这不会运行:
我不确定我能否弄清楚您想要实现的目标。即使您删除了“use strict;”警告,显示的代码未检测到
push
操作的更改。输出:
我不确定那里发生了什么。
Dealing with the revised notation:
we can immediately tell that you are not following the standard advice that protects novices (and experts!) from their own mistakes. You're using a symbolic reference, which is not a good idea.
This does not run:
I'm not sure I can work out what you were trying to achieve. Even if you remove the 'use strict;' warning, the code shown does not detect a change from the
push
operation.Output:
I'm not sure what is going on there.
如果您的问题是如何将之前存储的空字符串值替换为可以将值推入其中的数组,这可能是最好的方法:
%hash
中的整个事物。If your problem is how do you replace a empty string value you had stored before with an array onto which you can push your values, this might be the best way to do it:
%hash
.