我有一个问题希望你能帮忙,因为我是哈希和哈希引用的新手?
我有以下数据结构:
$VAR1 = {
'http://www.superuser.com/' => {
'difference' => {
'http://www.superuser.com/questions' => '10735',
'http://www.superuser.com/faq' => '13095'
},
'equal' => {
'http://www.superuser.com/ ' => '20892'
}
},
'http://www.stackoverflow.com/' => {
'difference' => {
'http://www.stackoverflow.com/faq' => '13015',
'http://www.stackoverflow.com/questions' => '10506'
},
'equal' => {
'http://www.stackoverflow.com/ ' => '33362'
}
}
如果我想访问密钥中的所有 URL,'difference'
然后我可以对 URL 执行一些其他操作,那么访问这些元素的正确或首选方法是什么?
例如,我最终会得到以下 URL,然后我可以在foreach
循环中执行以下操作:
http://www.superuser.com/questions
http://www.superuser.com/faq
http://www.stackoverflow.com/faq
http://www.stackoverflow.com/questions
- - - 编辑 - - -
访问上述数据结构下方元素的代码:
my @urls;
foreach my $key1 ( keys( %{$VAR1} ) ) {
print( "$key1\n" );
foreach my $key2 ( keys( %{$VAR1->{$key1}} ) ) {
print( "\t$key2\n" );
foreach my $key3 ( keys( %{$VAR1->{$key1}{$key2}} ) ) {
print( "\t\t$key3\n" );
push @urls, keys %{$VAR1->{$key1}{$key2}{$key3}};
}
}
}
print "@urls\n";
使用上面的代码为什么会出现以下错误?
Can't use string ("13238") as a HASH ref while "strict refs" in use at ....