如何使用 perl 遍历对象内的哈希数组
How to iterate through an array of hashes inside an object with perl
我正在尝试遍历存储为 'Person' 对象的“_skills”属性的哈希数组,但遇到了问题。
test1.pl
my $person = new Person( "Hubert", "Blu",
[
{"name" => "Python", "Weight" => 80, "group","IT SKILLS", "id"=>0002},
{"name" => "Magic", "Weight" => 100, "group", "MISC SKILLS", "id"=>0001},
{"name" => "JavaScript","Weight" => 70, "group"=> "IT SKILLS", "id"=>0004},
{"name" => "Mage","Weight" => 100, "group"=>"JOB TITLE", "id" =>0003},
{"name" => "Assassin", "Weight" => 85, "group"=>"JOB TITLE"}
]
);
和Person.pl
sub new {
my ($class,$first_name,$last_name,@skills) = @_ ;
my $self = {};
$self->{"_first_name"} = $first_name;
$self->{"_last_name"} = $last_name;
@{$self->{"_skills"}} = @skills;
# Print all the values just for clarification.
print "First Name is $self->{_first_name}\n";
print "Last Name is $self->{_last_name}\n";
print "Skills : " . Data::Dumper::Dumper($self->{_skills}) . "\n";
print"---SKILLS---\n";
my $i = 1;
foreach my $skill_set (@{$self->{_skills}}) {
print "skill ".$i .": \n";
$i++;
#print Data::Dumper::Dumper($skill_set) . "\n";
print "$skill_set->{'_first_name'}";
};
bless $self, $class;
return $self;
}
第一个转储显示数组,看起来不错。循环内的第二个转储再次显示整个数组,它只循环一次,然后出现以下错误
Not a HASH reference at /home/izoom/PerlTrainingGround/LeeR/lib/Person/Person.pm line 26.
被困了几个小时,一如既往地感谢您的帮助!
您将三个参数传递给 new
加上调用者。最后一个是对数组的引用。这意味着 @skills
以一个值为该引用的元素结束。
my $person = new Person( "Hubert", "Blu",
[
{"name" => "Python", "Weight" => 80, "group","IT SKILLS", "id"=>0002},
{"name" => "Magic", "Weight" => 100, "group", "MISC SKILLS", "id"=>0001},
{"name" => "JavaScript","Weight" => 70, "group"=> "IT SKILLS", "id"=>0004},
{"name" => "Mage","Weight" => 100, "group"=>"JOB TITLE", "id" =>0003},
{"name" => "Assassin", "Weight" => 85, "group"=>"JOB TITLE"}
]
);
应该是
my $person = new Person( "Hubert", "Blu",
{"name" => "Python", "Weight" => 80, "group","IT SKILLS", "id"=>0002},
{"name" => "Magic", "Weight" => 100, "group", "MISC SKILLS", "id"=>0001},
{"name" => "JavaScript","Weight" => 70, "group"=> "IT SKILLS", "id"=>0004},
{"name" => "Mage","Weight" => 100, "group"=>"JOB TITLE", "id" =>0003},
{"name" => "Assassin", "Weight" => 85, "group"=>"JOB TITLE"}
);
或
my ($class,$first_name,$last_name,@skills) = @_ ;
@{$self->{"_skills"}} = @skills; # Useless shallow array copy
# -or-
$self->{"_skills"} = [ @skills ]; # Same[1] but clearer
# -or-
$self->{"_skills"} = \@skills; # No copy of elements
应该是
my ($class,$first_name,$last_name,$skills) = @_ ;
@{$self->{"_skills"}} = @$skills; # Shallow array copy
# -or-
$self->{"_skills"} = [ @$skills ]; # Same[1] but clearer
# -or-
$self->{"_skills"} = $skills; # No copy of elements
- 因为此时
$self->{"_skills"}
是 undef
。
我正在尝试遍历存储为 'Person' 对象的“_skills”属性的哈希数组,但遇到了问题。
test1.pl
my $person = new Person( "Hubert", "Blu",
[
{"name" => "Python", "Weight" => 80, "group","IT SKILLS", "id"=>0002},
{"name" => "Magic", "Weight" => 100, "group", "MISC SKILLS", "id"=>0001},
{"name" => "JavaScript","Weight" => 70, "group"=> "IT SKILLS", "id"=>0004},
{"name" => "Mage","Weight" => 100, "group"=>"JOB TITLE", "id" =>0003},
{"name" => "Assassin", "Weight" => 85, "group"=>"JOB TITLE"}
]
);
和Person.pl
sub new {
my ($class,$first_name,$last_name,@skills) = @_ ;
my $self = {};
$self->{"_first_name"} = $first_name;
$self->{"_last_name"} = $last_name;
@{$self->{"_skills"}} = @skills;
# Print all the values just for clarification.
print "First Name is $self->{_first_name}\n";
print "Last Name is $self->{_last_name}\n";
print "Skills : " . Data::Dumper::Dumper($self->{_skills}) . "\n";
print"---SKILLS---\n";
my $i = 1;
foreach my $skill_set (@{$self->{_skills}}) {
print "skill ".$i .": \n";
$i++;
#print Data::Dumper::Dumper($skill_set) . "\n";
print "$skill_set->{'_first_name'}";
};
bless $self, $class;
return $self;
}
第一个转储显示数组,看起来不错。循环内的第二个转储再次显示整个数组,它只循环一次,然后出现以下错误
Not a HASH reference at /home/izoom/PerlTrainingGround/LeeR/lib/Person/Person.pm line 26.
被困了几个小时,一如既往地感谢您的帮助!
您将三个参数传递给 new
加上调用者。最后一个是对数组的引用。这意味着 @skills
以一个值为该引用的元素结束。
my $person = new Person( "Hubert", "Blu",
[
{"name" => "Python", "Weight" => 80, "group","IT SKILLS", "id"=>0002},
{"name" => "Magic", "Weight" => 100, "group", "MISC SKILLS", "id"=>0001},
{"name" => "JavaScript","Weight" => 70, "group"=> "IT SKILLS", "id"=>0004},
{"name" => "Mage","Weight" => 100, "group"=>"JOB TITLE", "id" =>0003},
{"name" => "Assassin", "Weight" => 85, "group"=>"JOB TITLE"}
]
);
应该是
my $person = new Person( "Hubert", "Blu",
{"name" => "Python", "Weight" => 80, "group","IT SKILLS", "id"=>0002},
{"name" => "Magic", "Weight" => 100, "group", "MISC SKILLS", "id"=>0001},
{"name" => "JavaScript","Weight" => 70, "group"=> "IT SKILLS", "id"=>0004},
{"name" => "Mage","Weight" => 100, "group"=>"JOB TITLE", "id" =>0003},
{"name" => "Assassin", "Weight" => 85, "group"=>"JOB TITLE"}
);
或
my ($class,$first_name,$last_name,@skills) = @_ ;
@{$self->{"_skills"}} = @skills; # Useless shallow array copy
# -or-
$self->{"_skills"} = [ @skills ]; # Same[1] but clearer
# -or-
$self->{"_skills"} = \@skills; # No copy of elements
应该是
my ($class,$first_name,$last_name,$skills) = @_ ;
@{$self->{"_skills"}} = @$skills; # Shallow array copy
# -or-
$self->{"_skills"} = [ @$skills ]; # Same[1] but clearer
# -or-
$self->{"_skills"} = $skills; # No copy of elements
- 因为此时
$self->{"_skills"}
是undef
。