Perl WWW:机械化访问散列引用中的数据
我有一个问题,希望你能帮我? 这是我在理解散列引用时需要帮助的最后一部分 代码:Perl WWW:机械化访问散列引用中的数据,perl,hash,www-mechanize,hashref,Perl,Hash,Www Mechanize,Hashref,我有一个问题,希望你能帮我? 这是我在理解散列引用时需要帮助的最后一部分 代码: my $content_lengths; # this is at the top foreach my $url ( # ... more stuff # compare if ( $mech->response->header('Content-Length') != $content_length ) { print "$child_url: different content length
my $content_lengths; # this is at the top
foreach my $url ( # ... more stuff
# compare
if ( $mech->response->header('Content-Length') != $content_length ) {
print "$child_url: different content length: $content_length vs "
. $mech->response->header('Content-Length') . "!\n";
# store the urls that are found to have different content
# lengths to the base url only if the same url has not already been stored
$content_lengths->{$url}->{'different'}->{$child_url} = $mech->response->header('Content-Length');
} elsif ( $mech->response->header('Content-Length') == $content_length ) {
print "Content lengths are the same\n";
# store the urls that are found to have the same content length as the base
# url only if the same url has not already been stored
$content_lengths->{$url}->{'equal'}->{$child_url} = $mech->response->header('Content-Length');
}
使用Data::Dumper时的样子
$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'
}
}
};
我需要什么帮助:
my $content_lengths; # this is at the top
foreach my $url ( # ... more stuff
# compare
if ( $mech->response->header('Content-Length') != $content_length ) {
print "$child_url: different content length: $content_length vs "
. $mech->response->header('Content-Length') . "!\n";
# store the urls that are found to have different content
# lengths to the base url only if the same url has not already been stored
$content_lengths->{$url}->{'different'}->{$child_url} = $mech->response->header('Content-Length');
} elsif ( $mech->response->header('Content-Length') == $content_length ) {
print "Content lengths are the same\n";
# store the urls that are found to have the same content length as the base
# url only if the same url has not already been stored
$content_lengths->{$url}->{'equal'}->{$child_url} = $mech->response->header('Content-Length');
}
我需要帮助理解访问散列引用中不同部分的各种方法,并使用它们来做一些事情,例如打印它们
例如,我如何从散列引用(即,从将要使用和的Data::Dumper)打印所有$url
如何打印$child\u url
中的全部或$child\u url
中的特定一个/子集,依此类推
非常感谢您的帮助,
非常感谢您可以浏览您的hashref,从而:
$hashref->{key1}{key2}{keyN};
例如,如果希望超级用户相等分支:
my $urlArrayref = $hashref->{'http://www.superuser.com/'}{'equal'};
更重要的是,要打印hashref的URL(第一级键),您需要执行以下操作:
foreach my $key ( keys( %{$hashref} ) ) {
print( "key is '$key'\n" );
}
然后,如果需要第二级关键点:
foreach my $firstLevelKey ( keys( %{$hashref} ) ) {
print( "first level key is '$firstLevelKey'\n" );
foreach my $secondLevelKey ( keys( %{$hashref->{$firstLevelKey}} ) ) {
print( "\tfirst level key is '$secondLevelKey'\n" );
}
}
等等
----编辑---
这是上面示例中的工作示例代码:
#!/usr/bin/perl
use strict;
use warnings;
my $content_lengths = {
'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'
}
}
};
foreach my $key1 ( keys( %{$content_lengths} ) ) {
print( "$key1\n" );
foreach my $key2 ( keys( %{$content_lengths->{$key1}} ) ) {
print( "\t$key2\n" );
foreach my $key3 ( keys( %{$content_lengths->{$key1}{$key2}} ) ) {
print( "\t\t$key3\n" );
}
}
}
这将导致此输出:
http://www.superuser.com/
difference
http://www.superuser.com/questions
http://www.superuser.com/faq
equal
http://www.superuser.com/
http://www.stackoverflow.com/
difference
http://www.stackoverflow.com/faq
http://www.stackoverflow.com/questions
equal
http://www.stackoverflow.com/
您好,您能告诉我我的示例的代码是什么样子的吗?当我使用您的示例作为模板执行
foreach
时,我会收到错误,例如“arg 1的类型必须是散列(而不是数组解引用)”?感谢您的帮助,如果我使用foreach my$here(key%{$content\u length}{print(“key is'$here'\n”);}
,我将无法获取一些数据?但是当我使用来处理我的$key(key(@{$content_length})){print(“key是'$key'\n”);
}?@perl用户时就不是了,很抱歉这是一个打字错误。。。应该是键(%{$content\u length})
,如您所知。我在上面更新了我的答案。@perl user,这回答了你的问题吗?@perl user,你一定是打错了什么。我测试了这段代码,请参见上面的编辑。它起作用了。