0

这是代码:

#!/usr/bin/perl -w
use strict;
use Data::Dumper;

my $var = { 
    Cars => {
        cars_name => [
            "GT500", 
            "Beetle",
            "P1800"
        ]
    },           
    Bikes => {
        Bikes_name => [
            "Pulsar",
            "Discover",
            "CBR"
        ]
    }
};

#Traversing the hash of hash of array
print "$var\n";
foreach my $k1 (keys %{$var}) {

    print "$k1\n";
    print $var->$k1;
    #foreach my $k2 (keys %{$var->$k1}) {

         #print"$k2\n";
    #}   
}   

每当我尝试打印 cars_name 和 Bikes_name 时,我都会收到一条错误消息,指出 Can't call method Cars on Unblessed reference。我无法弄清楚我在哪里做错了。好吧,根据我的理解print $var->$k1;线创建一个概率。所以请任何机构都可以帮助我解决这个问题。

谢谢

4

1 回答 1

4
 print $var->$k1;

即调用对象的方法。你想访问一个哈希:

 print $var->{$k1};
于 2012-09-28T07:26:10.873 回答