开发者

Hash to an Array in Perl

I'm working on creating a Graph class in Perl and I'm hoping to have each Graph contain Nodes. Each Node has some properties and also (for now) an array which contains references to each other node it is connected to. So far I have this definition of a node:

u开发者_运维百科se strict;
package Node;

sub new{
    my $class = shift;
    my @array = (1,2,3);
    my $array_r = \@array;
    my $self = {
        code => undef,
    name => undef,
    country => undef,
    continent => undef,
    timezone => undef,
    coordinates => ("", "", "", ""),
    population => undef,
    region => undef,
    arrayRef => $array_r,
    @_,
};
bless $self, $class;
return $self;
}

Yet upon calling the following function from my main script:

sub getSetArray{
my $self = shift;
return $self->{arrayRef};
}

and storing the value and printing it: my $val = $firstnode->getSetArray(); print @{$val};

I get the following error:

Can't use an undefined value as an ARRAY reference.

I'm guessing my understanding of the level of indirection of my references is off, but I can't seem to see whats going on.

Thanks, -A


Your ("","","","") is included in the key/value pairs in {}, throwing you off. If coordinates should be an arrayref too, you want ["","","",""].

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜