-2
我無法訪問散列值,我不知道我在做什麼錯誤。我做了一些Perl,但沒有太多的哈希。在散列哈希中訪問值
我試圖訪問散列哈希散列值。
這裏是我建的哈希
sub buildList
{
my ($name,$gender,$father,$mother,$age);
my %bear_ref=();
open IN, "<input.txt" or die "can't open file";
while(<IN>) {
($name, $gender, $father, $mother, $age) = split(/:/);
$bear_ref{ $name } { 'gender' } = $gender;
$bear_ref{ $name } { 'mother' } = $father;
$bear_ref{ $name } { 'father' } = $mother;
$bear_ref{ $name } { 'age' } = $age;
}
close IN;
return \%bear_ref;
}
當我通過按鍵中的散列itereting但不直接,我可以訪問列表。所以我假設它是與我是從獲取價值的方式「鍵()循環」
for my $name (keys %$ref) {
$father= $ref->{ $name }->{ 'father'}; # works
$mother= $ref->{ $name }->{ 'mother'}; # works
getTree($name, $ref);
}
sub getTree
{
my $bear = shift;
my $ref = shift;
my ($father, $mother);
$father= $ref->{ $name }->{ 'father'}; # doesn't work...have also tried with %$ref->
$mother= $ref->{ $name }->{ 'mother'}; # doesn't work...have also tried with %$ref->
print "$father : $mother\n";
}
任何幫助,將不勝感激。
總是['use strict;'](http://perldoc.perl.org/strict.html)和['use warnings;'](http://perldoc.perl.org/warnings.html)直到你確切知道它爲什麼被推薦。 –