Reputation: 6259
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
Upvotes: 2
Views: 131
Reputation: 107080
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.
Upvotes: 0
Reputation: 234847
You need to indicate that id
is a hash key. Try
print $persons[0]->{id};
Upvotes: 3