如何引用hashmap数组中的值?

发布于 2024-12-15 07:54:53 字数 266 浏览 0 评论 0原文

在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

韶华倾负 2024-12-22 07:54:53

您需要指出 id 是哈希键。尝试

print $persons[0]->{id};

You need to indicate that id is a hash key. Try

print $persons[0]->{id};
亣腦蒛氧 2024-12-22 07:54:53

现在,您已经掌握了 Perl 参考资料,您可能需要查看面向对象的 Perl

package Person;

sub new {
   my $class = shift;
   %params = @_;

   my $self = {};
   bless $self, $class;

   $self->Id($params{id});
   $self->Name($params{name});
   $self->Age($params{age});

   return $self;
}

sub Id {
   my $self = shift;
   my $id   = shift;

   if (defined $id) {
      $self->{ID} = $id;
   }
   return $self->{ID};
}

sub Name {
   my $self = shift;
   my $name = shift;

   if (defined $name) {
      $self->{NAME} = $name;
   }
   return $self->{NAME};
}

sub Age {
   my $self = shift;
   my $age  = shift;

   if (defined $age) {
      $self->{AGE} = $age;
   }
   return $self->{AGE};
}

现在,要定义一个新人,您可以执行以下操作:

my $person = Person->new(id => "3", name => "George", age => 29);

或者:

my $person = Person->new();
$person->Name("George");
$person->Id("3");
$person->Age(29);

并且,您可以像这样将它们推送到数组上:

push @persons, $person;

您可以像这样打印出他们的 id:

print $persons[0]->Id;

那么,为什么要经历所有这些麻烦呢?

让我们回到恐龙统治地球、每个人都在使用 Perl 3.0 编程的远古时代。那时,你还没有声明变量的概念。这意味着做到这一点非常容易:

$name = "bob";
print "Hello, my name is $Name\n";

哎呀!您分配了 $name,但您的 print 语句使用了 $Name

在 Perl 4.0 中,当我们现在可以使用严格和预声明变量时,这种情况就消失了:

use strict;
my $name = "bob";
print "Hello, my name is $Name\n";

这会产生错误,因为您没有声明$Name。您会看到错误消息,并立即解决问题。

现在,Perl 5.0 赋予我们使用引用的权利,我们再次回到 Perl 3.0,因为 Perl 5.0 中没有任何内容阻止我们使用未声明的哈希名称。

use strict;
my $person = {};
$person->{Name} = "bob";

print "My name is $person->{name}\n";

糟糕,没有哈希键 name。它是哈希键 Name

面向对象的 Perl 使您能够检查这些类型的错误:

my $person = Person->new();
$person->Name("Bob");
print "My name is " . $person->name;

这是一个错误,因为类成员函数(也称为子例程)name em>(又名包)人。

当你的程序变得越来越复杂时,你会发现它是一个救星。

当然,另一个原因是通过使用对象,我们可以限制代码中发生更改的位置。例如,假设您突然必须开始跟踪每个人的工资。如果没有面向对象的方法,您必须检查整个程序以了解如何操作 @persons 数组或其中的元素。

在面向对象的程序中,您所要做的就是创建一个名为 Salary 的新方法

sub Salary {
   my $self = shift;
   my $salary = shift;

   if (defined $salary) {
      $self->{SALARY} = $salary;
   }
   return $self->{SALARY};
}

这样就完成了。

当您更加熟悉这种 Perl 编程方法时,您会发现自己首先考虑程序处理的各种对象,然后编写首先是方法。您会发现编程速度更快且不易出错。

很抱歉这次演讲,但我花了很长时间使用散列数组的散列的散列,并浪费了大量时间试图让这些过于复杂的事情发挥作用。我一直希望有人能早点向我介绍面向对象的 Perl。

Now, that you have a hang of Perl references, you might want to look at Object Oriented Perl:

package Person;

sub new {
   my $class = shift;
   %params = @_;

   my $self = {};
   bless $self, $class;

   $self->Id($params{id});
   $self->Name($params{name});
   $self->Age($params{age});

   return $self;
}

sub Id {
   my $self = shift;
   my $id   = shift;

   if (defined $id) {
      $self->{ID} = $id;
   }
   return $self->{ID};
}

sub Name {
   my $self = shift;
   my $name = shift;

   if (defined $name) {
      $self->{NAME} = $name;
   }
   return $self->{NAME};
}

sub Age {
   my $self = shift;
   my $age  = shift;

   if (defined $age) {
      $self->{AGE} = $age;
   }
   return $self->{AGE};
}

Now, to define a new person, you do this:

my $person = Person->new(id => "3", name => "George", age => 29);

or:

my $person = Person->new();
$person->Name("George");
$person->Id("3");
$person->Age(29);

And, you can push them on the array like this:

push @persons, $person;

And you can print out their id like this:

print $persons[0]->Id;

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:

$name = "bob";
print "Hello, my name is $Name\n";

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:

use strict;
my $name = "bob";
print "Hello, my name is $Name\n";

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.

use strict;
my $person = {};
$person->{Name} = "bob";

print "My name is $person->{name}\n";

Whoops, there is no hash key name. It's hash key Name.

Object Oriented Perl gives you back the ability to check for these types of errors:

my $person = Person->new();
$person->Name("Bob");
print "My name is " . $person->name;

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:

sub Salary {
   my $self = shift;
   my $salary = shift;

   if (defined $salary) {
      $self->{SALARY} = $salary;
   }
   return $self->{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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文