如何引用hashmap数组中的值?
在perl中,如何引用hashmap数组中的值?例如,
my %person1 = (
id => 3,
name=> 'George',
age => 29,
);
my %person2 = (
id => 3,
name=> 'George',
age => 29,
);
my @persons = ( \%person1, \%person2 );
print $persons[0]->id; #wrong
In the perl, how to reference values in the array of hashmap ? for example,
my %person1 = (
id => 3,
name=> 'George',
age => 29,
);
my %person2 = (
id => 3,
name=> 'George',
age => 29,
);
my @persons = ( \%person1, \%person2 );
print $persons[0]->id; #wrong
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要指出
id
是哈希键。尝试You need to indicate that
id
is a hash key. Try现在,您已经掌握了 Perl 参考资料,您可能需要查看面向对象的 Perl:
现在,要定义一个新人,您可以执行以下操作:
或者:
并且,您可以像这样将它们推送到数组上:
您可以像这样打印出他们的 id:
那么,为什么要经历所有这些麻烦呢?
让我们回到恐龙统治地球、每个人都在使用 Perl 3.0 编程的远古时代。那时,你还没有声明变量的概念。这意味着做到这一点非常容易:
哎呀!您分配了
$name
,但您的 print 语句使用了$Name
。在 Perl 4.0 中,当我们现在可以
使用严格
和预声明变量时,这种情况就消失了:这会产生错误,因为您没有声明
$Name
。您会看到错误消息,并立即解决问题。现在,Perl 5.0 赋予我们使用引用的权利,我们再次回到 Perl 3.0,因为 Perl 5.0 中没有任何内容阻止我们使用未声明的哈希名称。
糟糕,没有哈希键
name
。它是哈希键Name
。面向对象的 Perl 使您能够检查这些类型的错误:
这是一个错误,因为类成员函数(也称为子例程)
name
em>(又名包)人。当你的程序变得越来越复杂时,你会发现它是一个救星。
当然,另一个原因是通过使用对象,我们可以限制代码中发生更改的位置。例如,假设您突然必须开始跟踪每个人的工资。如果没有面向对象的方法,您必须检查整个程序以了解如何操作
@persons
数组或其中的元素。在面向对象的程序中,您所要做的就是创建一个名为 Salary 的新方法:
这样就完成了。
当您更加熟悉这种 Perl 编程方法时,您会发现自己首先考虑程序处理的各种对象,然后编写类和首先是方法。您会发现编程速度更快且不易出错。
很抱歉这次演讲,但我花了很长时间使用散列数组的散列的散列,并浪费了大量时间试图让这些过于复杂的事情发挥作用。我一直希望有人能早点向我介绍面向对象的 Perl。
Now, that you have a hang of Perl references, you might want to look at Object Oriented Perl:
Now, to define a new person, you do this:
or:
And, you can push them on the array like this:
And you can print out their id like this:
So, why go through all of this trouble?
Let's go back to the ancient days when dinosaurs ruled the earth and everyone was programming in Perl 3.0. Back then, you didn't have the concept of declaring variables. This meant that it was very easy to do this:
Whoops! You assigned
$name
, but your print statement used$Name
.This went away in Perl 4.0 when we could now
use strict
and predeclared variables:That would produce an error because you didn't declare
$Name
. You'd see the error message, and immediately fix the problem.Now, here Perl 5.0 gives us the God given right to use references, and once more we go way back to Perl 3.0 because there's nothing in Perl 5.0 that prevents us from using undeclared hash names.
Whoops, there is no hash key
name
. It's hash keyName
.Object Oriented Perl gives you back the ability to check for these types of errors:
That's an error because there's no member function (aka subroutine)
name
in class (aka package) Person.As your programs get more complex, you'll find this as a lifesaver.
Another reason, of course, is that by using objects, we can limit where changes in our code occur. For example, let's say you've suddenly have to start tracking the salary of each person. Without an object oriented approach, you have to examine your entire program to see how you're manipulating your
@persons
array or the elements in it.In an object oriented program, all you have to do is create a new method called Salary:
And, you're all done.
As you get more familiar with this approach to Perl programming, you'll find yourself first thinking about the various objects your program deals with and then writing the classes and methods first. You'll find programming quicker and less error prone.
Sorry for the lecture, but I spent a long time using hashes of hashes of arrays of hashes, and wasting a lot of time attempting to get these overly complex things to work. I always wished someone pointed me to object oriented Perl a lot sooner.