0
객체 배열이 있습니다.Perl 해시에 객체 배열 할당하기
1) 어떻게 반복 할 수 있습니까? 나는 $ 노드 -> ID()가 될 각 요소에 대한 hash.The 키에 배열의 각 요소를 배치 할 수 있습니다 방법
for my $element (@nodeArray)
{
print $element;
}
2); 각 노드 요소의 값은 오브젝트의 특성이됩니다. 따라서 $ node-> id()가 반복됩니다.
코드는 여기에 있습니다 : 당신이 map
대신 foreach
루프로 사용할 수 있습니다
package person;
sub new {
my ($class) = @_;
my $self = {
_id => undef,
_name => undef,
_scores => []
};
bless $self, $class;
return $self;
}
sub id{
my ($self, $id) = @_;
$self->{_id} = $id if defined($id);
return $self->{_id};
}
sub name{
my ($self, $name) = @_;
$self->{_name} = $name if defined($name);
return $self->{_name};
}
sub scores {
my ($self, @scores)= @_;
if (@scores) {
@{ $self->{_scores} } = @scores;
};
return @{ $self->{_scores} };
}
use strict;
use warnings;
#use person;
use Data::Dumper;
my @nodeArray=undef ;
my %hash = undef;
my $node = eval{person->new();} or die ([email protected]);
$node->id(1);
$node->name('bob');
$node->scores(['34','1','1',]);
unshift(@nodeArray, $node) ;
$node = eval{person->new();} or die ([email protected]);
$node->id(2);
$node->name('bill');
$node->scores(['3','177','12',]);
unshift(@nodeArray, $node) ;
print Dumper (@nodeArray);
또한